• Resolved bghooke

    (@bghooke)


    I’m trying to create my own plug-in for a project and I need two custom post types. All seemed to be working well initially. I was able to declare the two post types and both show up in the admin area with their various custom taxonomies. I can create new posts using these custom types. But, whichever post type I declare first generates a page not found error when I try to view the post. Reversing the order of the declarations immediately results in a swap in which custom post type generates a 404 error.

    I initialize the custom post types with this code:

    [mis-formatted code redacted — see reply below]

    The complete code is available here: https://pastebin.com/KAvPr7DL

    I’ve been beating my head against this for hours and can’t figure out what’s going wrong. Any ideas? Thanks!

    • This topic was modified 5 years, 3 months ago by bcworkz. Reason: redaction

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter bghooke

    (@bghooke)

    My apologies. I used the wrong tags to identify the code snippets and there does not seem to be a way for me to edit the post to fix it. Here is the code again, I hope properly tagged:


    add_action( 'init', 'my_register_post_types_product' );
    add_action( 'init', 'my_register_post_types_project' );

    This calls the two functions that create the custom post types. Here are the key sections of that code:


    function my_register_post_types_product() {
    $args = array(
    ...
    'rewrite' => array(
    'slug' => 'product', // string (defaults to the post type name)
    'with_front' => false, // bool (defaults to TRUE)
    'pages' => true, // bool (defaults to TRUE)
    'feeds' => false, // bool (defaults to the 'has_archive' argument)
    'ep_mask' => EP_PERMALINK, // const (defaults to EP_PERMALINK)
    ),
    ...
    );
    register_post_type(
    'product', $args);}

    And:

    function my_register_post_types_project() {
    $args = array(
    ...
    'rewrite' => array(
    'slug' => 'project', // string (defaults to the post type name)
    'with_front' => false, // bool (defaults to TRUE)
    'pages' => true, // bool (defaults to TRUE)
    'feeds' => false, // bool (defaults to the 'has_archive' argument)
    'ep_mask' => EP_PERMALINK, // const (defaults to EP_PERMALINK)
    ),
    ...
    );
    register_post_type(
    'project', $args);}

    Moderator bcworkz

    (@bcworkz)

    I suspect you cannot have more than one post type where 'with_front'=>false. Without a front parameter in the permastruct, WP queries for the wrong post type with the requested post.

    Thread Starter bghooke

    (@bghooke)

    Thanks. Unfortunately that doesn’t solve the problem. I tried changing just one and also both to be 'with_front'=>true and the issue did not go away. After making that change I did go to the permalinks page and re-saved the settings there to flush the permalinks.

    Thread Starter bghooke

    (@bghooke)

    I seem to have found the solution. I had to explicitly set the query_var for both custom post types. This doesn’t really make sense to me as these should have been defaulting to the names of the custom post types, which is what I have now explicitly set these to be, but explicitly setting them solved the problem I was having.

    Thank you, bcworkz, for your suggestion. While it wasn’t the right solution it prompted me to look carefully at the settings for each of the variables I was using, which lead me to the solution.

    Moderator bcworkz

    (@bcworkz)

    No problem. It’s funny how a wrong suggestion can sometimes lead to the right solution ??

    While it’s not unusual in some cases to need to explicitly add CPTs to the query var, such as a category query, it shouldn’t be necessary for a single post query when the post type is part of the permalink. As long as you have it working now I guess it’s all good.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Conflict between two custom post type declarations’ is closed to new replies.