0

I have a site on WordPress 3.6.1 and I created a custom post type for recipes with the code:

 add_action( 'init', 'register_cpt_my_recipes' );
    function register_cpt_my_recipes() {
    $args = array(
    'hierarchical' => false,
    'supports' => array( 'title', 'editor', 'custom-fields' ,'tag' , 'excerpt', 'thumbnail'),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 5,
    'show_in_nav_menus' => false,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
     /* Not necessary but added as a solution */
    'rewrite'=> array( 'slug' => 'my_recipes', 'with_front' => true ),
    'capability_type' => 'post'
    );
    register_post_type( 'my_recipes', $args );
    /* Not necessary but added as a solution */
    flush_rewrite_rules( false );
    } 

I created archive-my_recipes.php custom template file but whenever I navigate to mysite/my_recipes/ I end up with code from archive.php with all my posts not just posts form my custom post type. I tried taxonomy-my_recipes.php but also didn't work .

also single-my_recipes.php is working fine.

I read about the subject on the WordPress documentation site here and here

This is the body_class output:

[ archive post-type-archive post-type-archive-Array logged-in admin-bar custom-font-enabled single-author customize-support ] 

I added :

add_action( 'pre_get_posts', 'add_my_post_types_to_query' );

function add_my_post_types_to_query( $query ) {
    if ( !is_admin() && $query->is_main_query() )
        $query->set( 'post_type', array( 'post', 'page', 'recipes' ) );
    return $query;
}

to function.php to include my custom post types in the main query.

10
  • Can we see a live example? Failing that, can you provide the actual URL, the non-rewritten URL, and the output of the body_class() function? – Chip Bennett Nov 12 '13 at 14:04
  • this is the body class output [ archive post-type-archive post-type-archive-Array logged-in admin-bar custom-font-enabled single-author customize-support ] – sh.e.salh Nov 12 '13 at 14:09
  • Please edit your question to add clarifying information, so that it doesn't get lost in the comments. – Chip Bennett Nov 12 '13 at 14:13
  • 1
    post-type-archive-Array is a problem. That should say post-type-archive-my_recipes. What else are you doing to modify the query for archive indexes? – Chip Bennett Nov 12 '13 at 14:15
  • 1
    "I added this part form the documentation :Custom Post Types in the Main Query" - I'm sorry, but I don't understand what you mean. Please edit your question to explain this detail. – Chip Bennett Nov 12 '13 at 14:58
1

Your pre_get_posts filter is incorrect. You are altering more queries than I think you think you are. The "main" query isn't just the primary index. You are adding (or attempting to add) all of those post types to every page, basically,-- the author archives, the categories, everything.

Try this:

add_action( 'pre_get_posts', 'add_my_post_types_to_query' );

function add_my_post_types_to_query( $query ) {
    if ( 
      !is_admin() 
      && $query->is_main_query() 
      && 'my_recipes' != $query->get('post_type')
    ) {
        $query->set( 'post_type', array( 'post', 'page', 'my_recipes' ) );
    }
}

Your archive-my_recipes.php should work now. I don't know if that the correct solution though. My suspicion is that you want is_home or is_front_page so that you only adding the additional post types to a particular index. Something like:

function add_my_post_types_to_query( $query ) {
    if ( 
      $query->is_home() 
      && $query->is_main_query()
    ) {
        $query->set( 'post_type', array( 'post', 'page', 'my_recipes' ) );
    }
}
1
  • I found that already and I used if ( !is_admin() && is_category() && $query->is_main_query() ) it work fine now as I just need to add it to categories only – sh.e.salh Nov 12 '13 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.