WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom taxonomy query-exclude post with specific term (4 posts)

  1. chachalady
    Member
    Posted 1 year ago #

    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.

  2. chachalady
    Member
    Posted 1 year ago #

    UPDATE:
    I add this line : 'post__not_in' => array(330,341),
    to exclude posts by their ID:

    <?php
             $args=array(
                   'post_type' => 'products',
    	       'taxonomy'=>'product-category',
                   'term' => 'usluge-proizvodi',
    	       'post__not_in' => array(330,341),
                   'post_status' => 'publish',
                   'posts_per_page' => 8
                          );
              query_posts( $args ); ?>	
    
    <?php while (have_posts()) : the_post(); ?>

    And it is working ok, those two posts are excluded.
    ( I thought post__not_in will not work with custom post type but it's working... )

    I need to create an array of posts ids so I dont have to add the id manually each time new post is written in 'akcije-novosti'.
    Need help with writing the code - there has to be a query/loop ? that goes through posts from 'akcije-novosti' and collect their id, right?

    After we create that array we can use it to exclude those ids in that line 'post__not_in' => array(..list of id...), I just need a little help with code, please ...

  3. chachalady
    Member
    Posted 1 year ago #

    UPDATE2: after I was sooooooo confused reading the codex and forums (it is so hard to find some informations for beginers...) I find something that helped me formulated and write the code.
    So this is working for me now:

    First loop on my page where I display posts from 'akcije-novosti':

    <?php
          // Create empty array to store post ids in
             $excludes = array();
    
                             $args=array(
                             'post_type' => 'products',
    			 'taxonomy'=>'product-category',
    			 'term' => 'akcije-novosti',
                             'post_status' => 'publish',
    			 'order' => 'DESC',
    			 'posts_per_page' => 2
                            );
                            query_posts( $args ); ?>	
    
        <?php while (have_posts()) : the_post(); ?>
    
                 <?php    // Add post to exclude array
    		      $excludes[] = $post->ID; ?>
    
            ...DO STUFF...DISPLAY TITLE, FEATURED IMAGE, EXCERPTS ....
    
      <?php endwhile; ?>

    So now those ids of the two newest posts from 'akcije-novosti' are stored in $excludes.

    Now, I have the second loop where I have to display posts from 'usluge-proizvodi' and exclude ids I stored in $excludes :

    <?php
             $args=array(
                   'post_type' => 'products',
    	       'taxonomy'=>'product-category',
                   'term' => 'usluge-proizvodi',
    	       'post__not_in' => $excludes,
                   'post_status' => 'publish',
                   'posts_per_page' => 8
                          );
              query_posts( $args ); ?>	
    
    <?php while (have_posts()) : the_post(); ?>
    
    ...DO STUFF...DISPLAY TITLE, FEATURED IMAGE, EXCERPTS ....
    
    <?php endwhile; ?>

    And it seems to work fine !!!!! :-)
    So as you can see I just needed to learn how to make that array with ids and use it ...
    Hope this code will help someone.
    Please give me a feedback or post reply or send me a message if you have something to add, and you think it is important.
    I will leave this topic open for a little while then will mark this as resolved.

  4. chachalady
    Member
    Posted 9 months ago #

    Ok..solved.

Topic Closed

This topic has been closed to new replies.

About this Topic