最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

display posts of custom post type with custom taxonomy

matteradmin11PV0评论

I have created a custom post type 'CPT_A' with CPT UI plugin. I have also created custom taxonomy 'Category_A' and 'Category_B' for 'CPT_A'. I have Posts 'Post_A', 'Post_B', 'Post_C', 'Post_D', 'Post_E' for 'CPT_A' and assigned 'Post_A', 'Post_B', 'Post_C' them to custom taxonomy 'Category_A' and 'Post_D', 'Post_E' to category 'Category_B'.

I have created archive page 'archive-CPT_A.php' to display all posts of 'CPT_A' post type. and WP archive.php handles posts listings for all Categories of the 'CPT_A' Posts. And there is 'single-CPT_A.php' to display contents of single 'CPT_A' Post.

Now, What I am trying to do is I have created a sidebar and trying to display related posts of 'CPT_A' in sidebar. If the post is from 'Category_A' it should display other posts from same category and if the post is from 'Category_B' it should display other posts from 'Category_B' So I used this code:

<?php
    $cats = get_terms('CPT_A');

    $args = array(
        'post_type' => 'CPT_A',
        'post__not_in' => array( get_the_ID() ),
        'posts_per_page' => 5,
        'cat'     => $cats[0]->term_id,
    );
    $query = new WP_Query( $args );

?>  

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

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

<?php endwhile; endif; wp_reset_postdata(); ?> 

But it's displaying hiding current post from list (which is correct) but also displaying other posts from 'Category_B' too. After some research I tried this code:

<?php

    $cats = get_terms('tour_category');

    $args = array(
        'post_type' => 'tour_package',
        'post__not_in' => array( get_the_ID() ),
        'posts_per_page' => 5,
        'cat'     => $cats[0]->term_id,
        'tax_query' => array(
            array(
                'taxonomy' => 'tour_category',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            )
            )
    );
    $query = new WP_Query( $args );

?>  

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

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

<?php endwhile; endif; wp_reset_postdata(); ?>

But it's displaying nothing for me.

What is the best way to do this? I didn't find any suitable tutorial for me so posting this question here.

I have created a custom post type 'CPT_A' with CPT UI plugin. I have also created custom taxonomy 'Category_A' and 'Category_B' for 'CPT_A'. I have Posts 'Post_A', 'Post_B', 'Post_C', 'Post_D', 'Post_E' for 'CPT_A' and assigned 'Post_A', 'Post_B', 'Post_C' them to custom taxonomy 'Category_A' and 'Post_D', 'Post_E' to category 'Category_B'.

I have created archive page 'archive-CPT_A.php' to display all posts of 'CPT_A' post type. and WP archive.php handles posts listings for all Categories of the 'CPT_A' Posts. And there is 'single-CPT_A.php' to display contents of single 'CPT_A' Post.

Now, What I am trying to do is I have created a sidebar and trying to display related posts of 'CPT_A' in sidebar. If the post is from 'Category_A' it should display other posts from same category and if the post is from 'Category_B' it should display other posts from 'Category_B' So I used this code:

<?php
    $cats = get_terms('CPT_A');

    $args = array(
        'post_type' => 'CPT_A',
        'post__not_in' => array( get_the_ID() ),
        'posts_per_page' => 5,
        'cat'     => $cats[0]->term_id,
    );
    $query = new WP_Query( $args );

?>  

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

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

<?php endwhile; endif; wp_reset_postdata(); ?> 

But it's displaying hiding current post from list (which is correct) but also displaying other posts from 'Category_B' too. After some research I tried this code:

<?php

    $cats = get_terms('tour_category');

    $args = array(
        'post_type' => 'tour_package',
        'post__not_in' => array( get_the_ID() ),
        'posts_per_page' => 5,
        'cat'     => $cats[0]->term_id,
        'tax_query' => array(
            array(
                'taxonomy' => 'tour_category',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            )
            )
    );
    $query = new WP_Query( $args );

?>  

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

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

<?php endwhile; endif; wp_reset_postdata(); ?>

But it's displaying nothing for me.

What is the best way to do this? I didn't find any suitable tutorial for me so posting this question here.

Share Improve this question asked Oct 26, 2018 at 4:56 Milan BastolaMilan Bastola 2972 gold badges4 silver badges15 bronze badges 2
  • 1 cat is not a general taxonomy query var, it is specifically for the built-in category taxonomy, and won't work with a custom taxonomy. In your second code block, you use $custom_term, but you never give it a value. – Milo Commented Oct 26, 2018 at 15:51
  • oh. it's because I am just giving it a try. Can you suggest me the solution? – Milan Bastola Commented Oct 26, 2018 at 15:57
Add a comment  | 

1 Answer 1

Reset to default 1

I would use "get_the_terms" to get the terms related to the post instead of "get_terms", here is a quick suggestion (not tested)

    $args=array(  
        'post_type'=>'tour_package',                 
        'post__not_in' => array(get_the_ID()),
        'posts_per_page'=> 5, 
        'caller_get_posts'=>1,//handle sticky post but not in first
    ); 

    $categories = get_the_terms( get_the_ID(),'tour_category' );

    if ($categories)
            {   
                $category_ids = array();         
                foreach($categories as $individual_category){
                   $category_ids[] = $individual_category->term_id; 
                }
                if(!empty($category_ids))
                {

                    $args['tax_query']= array(
                        array(
                            'taxonomy' => 'tour_category',
                            'field'    => 'term_id',
                            'terms'    => $category_ids,
                        ),
                    );
                }
            }
$query = new WP_Query( $args );
Post a comment

comment list (0)

  1. No comments so far