1
votes

I am having issues with custom post type categories display. I have created custom post type for review site I want to show different categories in different tabs but when I put any category of review in menu it shows all reviews instead of showing reviews from specific category For example: I have created 2 categories in reviews a) Games b) software Whenever I choose Games category it shows posts from software category too.

I had same issue with blog posts categories but I resolved that issue using code in my category.php file

 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
         $cat_id = get_cat_ID( single_cat_title(null, false) );
                 query_posts(array(
        'post_type'      => 'post',
        'paged'          => $paged,
        'cat'=>$cat_id,

        ));

I have created taxonomy.php file for custom post type

<?php $mypost = array( 'post_type' => 'cpreviews','paged' => $paged);
$loop = new WP_Query( $mypost ); ?>

Can anyone please help us to understand what we need to do to display posts according to categories for custom post types?

UPDATED CODE IN TAXONOMY.PHP but still have some issue:

I have changed above code under taxonomy.php to

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
          //$currentTerm = $_GET[ 'term' ]; 
          $cat_id = get_cat_ID( single_cat_title(null, false) );
          $mypost = array('post_type' => 'cptreviews',
                      'paged' => $paged,
                      'tax_query' => array(
                            array(
                                'taxonomy' => 'product_reviews_product_category',
                                'terms' => (''),
                                'field' => 'slug',
                                'name' =>'Product Category',
                                )
                            )
                        );
$loop = new WP_Query( $mypost ); ?>

Now whenever I put category in 'terms' => ('kids') like this it shows all posts under that category only. but I want to take that 'terms value' dynamically.

4

4 Answers

1
votes

Try this one:

<?php
$type = 'cpreviews';
$args=array(
  'post_type' => $type,
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></p>
    <?php
  endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>
0
votes

Supposing you have Custom Post Type: cpReviews --- Custom Taxonomy: RevCategories --- Create new review post and chose category from RevCategories. Query cpReviews will definitely show all the posts, you need to do some thing like this -----

query_posts(array(
        'post_type' =>'cpreviews', //Custom_Post_TYpe
        'showposts' => $limit,  
        'RevCategories' => 'Games',));  //Custom Post Type TAxonomy (can use page name here get_query_var('pagename'); for dynamic content
while (have_posts()): the_post(); global $post; echo the_title(); endwhile;           
0
votes

I have resolved this issue by creating taxonomy-{taxonomy}.php file & removed tax query code..it automatically takes given category..thanks all for your help

0
votes

This will resolve this issue.

$args = array(
'post_type'=> 'post',
'cat' => 'Games'
);              

$the_query = new WP_Query( $args );
if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();