admin管理员组文章数量:1320878
I use this query to call posts from a custom-type post I setup apart
$args = array( 'posts_per_page' => 5, 'order'=> 'DES', 'post_type' => 'custom-post' );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<div class="thumbnail">
<?php echo get_the_post_thumbnail( $post->ID, 'thumbnail' ); ?>
</div>
<?php the_title( sprintf( '<h2><a href="%s">', esc_url( get_permalink() ) ),'</a></h2>' ); ?>
<?php echo '<p>' . get_the_excerpt() . '</p>' ?>
<?php endforeach;
wp_reset_postdata();
Two questions:
- Is that correct or can I improve it better somehow?
- Is it possible to call posts that are placed both in 2 different taxonomy generated from that custom post type ("flowers" and "colors" for instance) and then call post that is in the "flowers" category but not in the "colors" one?
Thank you
I use this query to call posts from a custom-type post I setup apart
$args = array( 'posts_per_page' => 5, 'order'=> 'DES', 'post_type' => 'custom-post' );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<div class="thumbnail">
<?php echo get_the_post_thumbnail( $post->ID, 'thumbnail' ); ?>
</div>
<?php the_title( sprintf( '<h2><a href="%s">', esc_url( get_permalink() ) ),'</a></h2>' ); ?>
<?php echo '<p>' . get_the_excerpt() . '</p>' ?>
<?php endforeach;
wp_reset_postdata();
Two questions:
- Is that correct or can I improve it better somehow?
- Is it possible to call posts that are placed both in 2 different taxonomy generated from that custom post type ("flowers" and "colors" for instance) and then call post that is in the "flowers" category but not in the "colors" one?
Thank you
Share Improve this question edited Sep 29, 2020 at 19:45 Hector 6821 gold badge7 silver badges18 bronze badges asked Sep 29, 2020 at 13:50 PaoloPgnPaoloPgn 12 Answers
Reset to default 0Answering your second question - yes, it is possible, you can use tax_query for this, for example, this query will get all posts that has any term of 'flowers' taxonomy AND 'colors' taxonomy:
$query = new WP_Query( array(
'post_type' => 'custom-post',
'posts_per_page' => 5,
'order' => 'DESC',
'tax_query' => array(
'relation' => 'AND', // it is also possible to use OR
array(
'taxonomy' => 'flowers',
'operator' => 'EXISTS'
),
array(
'taxonomy' => 'colors',
'operator' => 'EXISTS'
)
)
) );
The second query will get all posts that have terms from 'flowers' taxonomy, but not from 'colors' taxonomy:
$query = new WP_Query( array(
'post_type' => 'custom-post',
'posts_per_page' => 5,
'order' => 'DESC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'flowers',
'operator' => 'EXISTS'
),
array(
'taxonomy' => 'colors',
'operator' => 'NOT EXISTS' // <-- Take a look at this operator
)
)
) );
The code is not tested
Here are also links to the Documentation, check out how to use WP_Query https://developer.wordpress/reference/classes/wp_query/#standard-loop
And also WP_Tax_Query https://developer.wordpress/reference/classes/wp_query/#taxonomy-parameters
Testing your code I arrived finally to an answer: In the first query I didn't use any 'operator' and it works! In the second query I used the 'operator' 'IN' and 'NOT IN' and it worked! Thank you
本文标签: Custom post type query with taxonomy
版权声明:本文标题:Custom post type query with taxonomy 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742089158a2420160.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论