admin管理员组文章数量:1130349
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 Answer
Reset to default 1I 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 );
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
catis not a general taxonomy query var, it is specifically for the built-incategorytaxonomy, 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
1 Answer
Reset to default 1I 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 );
本文标签: display posts of custom post type with custom taxonomy
版权声明:本文标题:display posts of custom post type with custom taxonomy 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749229604a2336218.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


catis not a general taxonomy query var, it is specifically for the built-incategorytaxonomy, 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