Custom Post Type with 404 error

Asked

Viewed 390 times

1

I have a sub page listing some Custom Posts.

The sub page opens normally and lists the custom posts, but when I click to open the post gives page not found.

Registration of the Custom Post:

add_action( 'init', 'register_cpt_injecao' );
function register_cpt_injecao(){
$labels = array( 
    'name' => 'Cursos de Injeção de Plásticos',
    'singular_name' => 'Curso',
    'add_new' => 'Add Novo',
    'add_new_item' => 'Add Novo Curso',
    'edit_item' => 'Editar Cursos de Injeção de Plásticos',
    'new_item' => 'Novo Cursos de Injeção de Plásticos',
    'view_item' => 'View Curso',
    'search_items' => 'Buscar Cursos de Injeção de Plásticos',
    'not_found' => 'Nenhum Curso encontrado',
    'not_found_in_trash' => 'Nenhum Curso na lixeira',
    'parent_item_colon' => 'Parent Curso:',
    'menu_name' => 'Cursos de Injeção de Plásticos',
);

$args = array( 
    'labels' => $labels,
    'hierarchical' => false,
    'supports' => array( 'title', 'editor', 'thumbnail'),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => array("slug" => "cursos/cursos-de-injecao-de-plasticos"),
    //'rewrite' => true,
    'capability_type' => 'post'
);

register_post_type( 'injecao', $args );
}

If I leave the rewrite set as TRUE, the page single-shot.php opens normal displaying its content, but then what happens is that the sub page (courses/courses-of-injection-plastic) gives error 404...

Any solution to this URL rewriting problem? I tried several things but when one works the other gives error, I don’t know what else to do.

1 answer

1


Marcelo, according to the Codex, in function register_post_type():

'Slug' => string Customize the permalink Structure Slug. Defaults to the $post_type value. Should be translatable.

In my mind, that means the following:

By default, your custom post’s Slug will be the $post_type (in your case, injecao). Permalinks work by defining the slug. With the rewrite setado to false, you allow WP to interpret any path /injecao as a path to your custom post. If you rewrite it (your case), it ignores the default and forces what you set. According to this thread, slugs nay may contain characters such as /. Your 404 is probably due to an error related to this.

To get around this problem, along with the known 404s problem in custom posts, there are a few points to consider:

  1. Rename your Custom Post to cpt_injecao, or any other prefix you want.

    This way you avoid confusion. The name cpt_injecao will only be used to reference the creation of the post in your code. If you want to define a taxonomy at another time, use the name with the prefix, so that you know what you are dealing with. Another advantage? You can

  2. Use rewrite rule as 'rewrite' => array("slug" => "injecao"). So the word injecao now is the reference for your custom post dealing with permalinks.

    This change, of course, can impact some business rules. I don’t know how, within the structure of your page, you are working on your custom post (i.e., Is there a page with a loop for all of them? What is the name of that page?). According to your own question (and according to the collusion I took on top of it), there is a page called courses. Then will she present the loop of all posts, and within it you will list the permalinks for your hypothetical single-cpt_injecao.php. This can impact in such a way that maybe it’s smart for you to create a new CP called courses (all this, of course, based on my opinion). It is interesting that you reevaluate these rules because:

  3. Custom Posts and pages may not have the same name!!!

    In a heated discussion that can be followed here, it is possible to see that many people have had this problem (and people who still have). If you create a CP called books, and then a page called books, uses the page to list the Cps and still uses the permalinks structure, the WP gets lost. It does not know what is post and what is page, due to the rewrite, And then BOOM! 404.

My suggestion then: first review your page structure, and how it will be used to call your Cps. I advise you not to use a page with the same name as CP, to avoid the problem mentioned in the discussion. And finally, if you’re still having problems with 404, flush your permalink rules. To do this, just go into the setup, save it as anything else and then go back to where you were (probably your is set to 'Post Name'). That way, you’re avoiding the dreaded 404.

I’ve had this problem before, and I had to do basically the same process that I described here. I didn’t take any tests, but I believe my answer will elucidate your work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.