Creating a multi-taxonomy query with an offset

Apologies if this has been asked before, but I couldn't find, specifically, another question that gets me going in the right direction.

I'm creating a series of multiple loops on a page. I also created another taxonomy for posts, "Issue". One of the loops I'm trying to create, I'd like to query this taxonomy and a particular category, "featured". But taking that a step further, I first need to figure out the "newest" issue and then offset by one.

Example: In my custom taxonomy, I have terms "Issue 1", "Issue 2", and "Issue 3". All created chronologically. This loop should find the newest term in this taxonomy (read: highest ID), then exclude it from the loop, then given what's left, only show posts in the category "Featured".

Here's the code I've code up with thus far:

$issue = get_terms('issue', array(
    'orderby' => 'none',
    'order' => 'DESC',
    'number' => 1,
    'offset' => 1
    )
);
$latest_issue = $issue->slug;

$args = array(
    'post_type' => 'post',
    'posts_per_page' => '5',
    'order_by' => 'date',
    'order' => 'DESC',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => array( 'featured' ),
        ),
        array(
            'taxonomy' => 'issue',
            'field' => 'slug',
            'terms' => array( $latest_issue )
        )
    )
);

$query = new WP_Query( $args );

echo '<div class="home-middle widget-area">';
echo '<h4 class="widget-title widgettitle"><span>From the Archives</span></h4>';

if (have_posts()) {
    while( $query->have_posts() ) { $query->the_post();
        echo '<h4>' . get_the_title() . '</h4>';
    }
} else {
    echo 'No posts in the Archive';
}

echo '</div>';

wp_reset_postdata();
  • Nithin

    Hi NWTD,

    Hope you are doing good today. :slight_smile:

    Could you please try the following code, and check whether it works according to your given example:

    <?php
    $issue = get_terms('issue', array(
        'orderby' => 'none',
        'order' => 'DESC',
        'number' => 1,
        'offset' => 1
        )
    );
    $latest_issue = $issue->slug;
    
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => '5',
        'order_by' => 'date',
        'order' => 'DESC',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'category',
                'field'    => 'slug',
                'terms'    => array( 'featured' ),
            ),
            array(
                'taxonomy' => 'issue',
                'field' => 'slug',
                'terms' => array( $latest_issue )
            )
        )
    );
    
    $query = new WP_Query( $args );
    
    echo '<div class="home-middle widget-area">';
    echo '<h4 class="widget-title widgettitle"><span>From the Archives</span></h4>';
    
    if ( $query->post_count > 0 ):
        foreach ( $query->get_posts() as $post ): ?>
            <h4><?php echo $post->post_title ?></h4>
        <?php endforeach; ?>
    <?php else: ?>
       <h4> No posts in the Archive</h4>
    <?php endif; ?>
    
    </div>
    
    <?php wp_reset_postdata(); ?>

    I hope this helps. Please let us know if you still need any further assistance. Have a nice day. :slight_smile:

    Kind Regards,
    Nithin

    • NWTD

      Ash Thanks for the quick reply, but it seems to be a no go. When I try your suggestions, I get the following error:

      Notice: Undefined offset: 0 in /var/www/html/development/wp-content/themes/magazine-pro/front-page.php on line 118
      
      Notice: Trying to get property of non-object in /var/www/html/development/wp-content/themes/magazine-pro/front-page.php on line 118

      I tried removing 'offset' => 1 from $issue = get_terms ('issue', array ...`

      When I did this, the query resulted in displaying the same post 5 times. You can see the query in action at the bottom of the page here: http://csl.nsta.org/development/

  • NWTD

    Maybe my query is wrong, as a whole, for what I'm trying to accomplish and should be re-evaluated.

    The function should get all the terms for the custom taxonomy "issue". Then find the newest one, or the one with the highest post ID. Then to create the query, we should get 5 posts, order them randomly (I realized my original code posted reads DESC), and they should be of the post category "Featured" and should be in any taxonomy of issue except for the (newest) one with the highest post ID.

  • Ash

    Hello NWTD

    Sorry, I am getting confused! :slight_frown:

    Let me try to rephrase your requirement and you made the correction if needed.

    1. Get all terms of taxonomy issue, issue is a custom taxonomy if "post" post type.
    2. Find the latest created term, say the term is ABC
    3. Get 5 posts which is from "featured" category and has term "ABC", order them randomly.

    Is that something you want?

    Have a good day!

    Cheers
    Ash

    • NWTD

      Ash, Sorry for the confusion.

      The function will be multiple part using two different taxonomies of posts: categories and issues

      1. Get all terms of the taxonomy issue.
      2. Determine and set as a variable the newest taxonomy term of issue (I currently have "Issue 1", "Issue 2", and "Issue 3", all created chronologically. "Issue 3" is the newest taxonomy term of issue).
      3. Create a query that uses both categories and issues; in that query, exclude the taxonomy term that was found in #2 above.

      Hopefully that's a little more clear. If not, let me know.

      Thanks again!

  • NWTD

    I got it working using the following:

    $issue = get_terms(array(
    		'taxonomy' => 'issue',
    		'hide_empty' => false,
    		'orderby' => 'id',
    		'order' => 'DESC',
    		'number' => 1
    		)
    	);
    
    	$latest_issue = $issue[0]->slug;
    
    	$args = array(
            'post_type' => 'post',
    		'orderby' => 'rand',
    		'posts_per_page' => '4',
            'tax_query' => array(
    			'relation' => 'AND',
    			array(
    				'taxonomy' => 'category',
    				'field'    => 'slug',
    				'terms'    => array( 'featured' ),
    			),
                array(
    				'taxonomy' => 'issue', // My Custom Taxonomy
    				'terms' => array( $latest_issue ), // My Taxonomy Term that I wanted to exclude
    				'field' => 'slug', // Whether I am passing term Slug or term ID
    				'operator' => 'NOT IN',
                )
            )
        );
    
        $query = new WP_Query( $args );
    
    	echo '<div class="home-middle widget-area">';
    	echo '<h4 class="widget-title widgettitle"><span>From the Archives</span></h4>';
    	echo '<section class="featured-content featuredpost">';
    
    	if ($query->have_posts()) {
    		while ($query->have_posts()) {
    		$query->the_post();
    			echo '<article class="post type-post status-publish format-standard has-post-thumbnail entry">';
    			echo '<a href="' . get_the_permalink() . '" class="alignleft" aria-hidden="true">';
    			echo the_post_thumbnail( 'home-middle' );
    			echo '</a>';
    			echo '<h4>' . get_the_title() . '</h4>';
    			echo get_the_excerpt();
    			echo '</article>';
    		}
    	} else {
    		echo 'No posts in the Archive';
    	}
    
    	echo '</section></div>';
    
    	wp_reset_postdata();