0
votes

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. My custom post type's post appear on the main loop.

I read about the subject on the wordpress documentation site here and here I also read answers for similar question here but no working solutions yet.

Also tried the solution here :

function namespace_add_custom_types( $query ) {
  if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'post_type', array(
     'post', 'your-custom-post-type-here'
        ));
      return $query;
    }
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );
2
copy archive file and give name of your file archive-{custompost type name}.php like archive-register_cpt_my_recipes.php - Ravi Patel
@ravi you didn't read the question , I did that and its not working the question was why. - sh.e.salh

2 Answers

0
votes

I feel your problem is similar to this custom post types and template names

you may need to update the permalink settings

0
votes

Ok , I found it this part cause the problem

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;
}

when I remove it archive page work normally however now I have to edit other templates to get the custom post by providing query arguments.

if anyone asking how you can use this in your template instead of the default loop

<?php $loop = new WP_Query( array( 'post_type' => 'recipes', 'posts_per_page' => 100 ) ); ?>

       <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>



        <ul>
            <li>

                <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>

                <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>

            </li>
        </ul>



            <?php endwhile; // end of the loop. ?>