Custom Post Type with error 404

1

I have a sub page listing some Custom Posts.

The sub page normally opens and lists the custom posts, but when I click to open the page it does not find the page.

Custom Posting Registry:

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 to TRUE , the single-injection.php page opens normal displaying its content, but then what happens is that the sub page / plastic-injection-courses) gives 404 error ...

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

    
asked by anonymous 08.05.2015 / 20:59

1 answer

1

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

  

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

In my understanding, this means the following:

By default, the slug of your custom post will be $post_type (in this case, injecao ). The permalinks work by setting slug . With rewrite set to false , you allow WP to interpret any /injecao path 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 no may contain characters as / . Your 404 probably comes from some error related to this.

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

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

    In 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 you know what you're dealing with. Another advantage? You can

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

    This change, of course, can impact some business rules. I do not know how, within the structure of your page, you are working 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 constipation I took upon it), there is a page called courses . So will it show the loop of all posts, and inside it you will list the permalinks for your hypothetical single-cpt_injecao.php . This can impact in such a way that it might be smart to create a new CP called courses (this of course based on my opinion). It's interesting that you reevaluate these rules because:

  • Custom Posts and pages can not have the same name !!!

    In a heated discussion that can be followed here , you can see that a lot of people have had this problem (and people who still have it). If you create a CP named books , and then a page named books , uses the page to list the CPs and still uses the permalink structure, WP is lost. It does not know what post is and what page it is, due to 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 the CP, to avoid the problem mentioned in the discussion. And finally, if you're still having problems with 404, flush your permalinks rules. To do this, just go into the configuration, save it as anything else and then go back to where it was (probably yours 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 I described here. I did not take any tests, but I believe that my answer will elucidate your work.

        
    12.05.2015 / 22:36