Custom taxonomy query-exclude post with specific term
-
Hello, I need help with my query. For now, I can get/retrieve and display the posts from certain custom taxonomy with specific term, but some of those posts are also/in the same time “within other term” that I woulld like to exclude.
Here is the situation:
– custom post type is ‘products’
– within products I have ‘taxonomy’=>’product-category’
(like post categories I can create in admin area) and two of them are called (their “names/slugs” are): ‘usluge-proizvodi’ and ‘akcije-novosti’
– some of the posts(products) are in the same time “within” ‘usluge-proizvodi’ and ‘akcije-novosti’ (like when the normal posts have two categories)
– I need to display all posts from ‘usluge-proizvodi’ EXCLUDING posts with term ‘akcije-novosti’ (within ‘akcije-novosti’)I had no problem to display ‘akcije-novosti’ separately.
After displaying those posts on homepage (custom homepage template) I have to display latest posts from ‘usluge-proizvodi’ excluding those from ‘akcije’.Here is the code I tested, without exclude, that displays posts from ‘usluge-proizvodi’, it is working ok:
<?php $args=array( 'post_type' => 'products', 'taxonomy'=>'product-category', 'term' => 'usluge-proizvodi', 'post_status' => 'publish' 'posts_per_page' => 8 ); query_posts( $args ); ?> <?php while (have_posts()) : the_post(); ?>
I tried something like this to display ‘usluge-proizvodi’ excluding ‘akcije-novosti’ :
<?php $wpq=array( 'post_type' => 'products', 'post_status' => 'publish', 'posts_per_page' => 8, 'tax_query'=> array('relation' => 'AND', array( 'taxonomy'=>'product-category', 'term' => 'usluge-proizvodi', 'operator' => 'IN' ), array( 'taxonomy' => 'product-category', 'term' => 'akcije-novosti', 'operator' => 'NOT IN' ) ) ); $filteredlist = new WP_Query($wpq); ?> <?php while ( $filteredlist->have_posts() ) : $filteredlist >the_post(); ?>
But this doesn’t return nothing at all. I also tried some variations and either nothing comes out or it displays all posts from ‘usluge-proizvodi’ withouth excluding ‘akcije-novosti’.
I am still not enough familiar with syntax and codex, I guess this way is not the right way, I missed something …
I already did something similar with normal posts and categories, but I know that we can not do the same with custom post type …And mostly I did everything this way: query_posts(‘numberposts=5&category_name=news&category__not_in=15’); …etc
But I would like to learn Advanced Taxonomy queries.Here is what I can see when I rollover my “custom so-called” categories like ‘akcije-novosti’:
…/wp-admin/edit-tags.php?action=edit&taxonomy=product-category&tag_ID=10&post_type=products
You can see that ID of ‘akcije-novosti’ is 10. Slug is ‘akcije-novosti’.Maybe I need two queries, one to make an array with post IDs from ‘akcije-novosti’, and THEN exclude those posts from other query using their IDs?
If that is so I need your help with writing the code:
– how to get those IDs and put it in array / or variable
(I really need help with syntax)
– how to use it to exclude them from other query where I get posts from ‘usluge-proizvodi’
I am working on someone elses wordpress theme, helping them , this is the last “big” issue we have before finishing the work.
If I had more time I would study the codex and forums more deeply, but this is almost an emergency call for help.
Thank you all so wery much.
- The topic ‘Custom taxonomy query-exclude post with specific term’ is closed to new replies.