1

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();


UPDATE EDIT

Thanks @adelval for pointing out the errors in the query. I've since updated that and have made some progress. My problem now is that my query gets only the posts in the newest (by newest, I mean the one with the highest taxonomy ID) taxonomy term and then displays it 5 times. You can see it in action, at the bottom of the page, here (under the section heading From the Archives). And here's the new code:

$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',
    '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>';

if ( $query->post_count > 0 ){
    foreach ( $query->get_posts() as $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 '<time class="entry-time" itemprop="datePublished">' . get_the_date() . '</time>';
        echo '</a>';
        echo '<h4>' . get_the_title() . '</h4>';
        echo get_the_excerpt();
        echo '</article>';
    endforeach;
} else {
    echo 'No posts in the Archive';
}

echo '</div>';

wp_reset_postdata();

To be clear on what I'm trying to accomplish, this function should:

  1. Get all terms of the taxonomy issue. Done
  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). Done
  3. Create a query that uses both categories and issues; in that query, exclude the taxonomy term that was found in #2 above. Needs refinement

Thanks for the help!

1
+50

Have you considered using a more standard loop, like this? I think it might fix your issue #3.

$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->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 '<time class="entry-time" itemprop="datePublished">' . get_the_date() . '</time>';
        echo '</a>';
        echo '<h4>' . get_the_title() . '</h4>';
        echo get_the_excerpt();
        echo '</article>';
    }
} else {
    echo 'No posts in the Archive';
}

echo '</div>';

wp_reset_postdata();
|improve this answer|||||
  • Wow. It was just that easy. Someone in a different thread I posted suggested the foreach style loop and i never changed it back in troubleshooting. Thank you! – NW Tech Nov 9 '16 at 23:00
1

Wordpress doesn't keep track of creation date for taxonomy terms, so you cannot sort them by date or ask for the most recently created one. So you cannot sort issues if issues is a taxonomy.

To achieve what I understand you want to achieve, you can either create a custom post type for "issues" - and then the "issue posts" (titled issue 1, issue 2,...) can be sorted by date; or simply create a post category (Posts->Categories) for issues. Then restrict the queries in the loops you mention by either post_type, in the former case, or by 'category' => 'issue' in the latter; and 'offset' => '1' to discard the most recent post/issue from the loop in either case.

A custom post type 'issue' would be preferable if you expect to associate quite a few meta properties (post_meta) to this kind of post, specially if you want the meta fields to be easily set with metaboxes in the admin (or maybe by users filling a form). Normal posts of category 'issue' may be simpler though, and you can still add post meta to them in their editing screen.

On a side note, according to https://developer.wordpress.org/reference/functions/get_terms/ since 4.5.0 your syntax for get_terms if slightly off and won't work. Also, the 'offset' arg should be in var $args, not in the get_terms parameters.

Feel free to clarify if I'm misunderstanding what you mean.

|improve this answer|||||
  • I understand that, I was more hoping to sort them by their tag ID. 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 more clear. If not, let me know. – NW Tech Nov 9 '16 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.