0

I'm loading posts via a query of a custom taxonomy called 'band':

$args = array(
'tax_query' => array(
    array(
        'taxonomy' => 'band',
        'field' => 'slug',
        'terms' => array('learn', 'trade', 'invest', 'spend')
        )
    ),
'post_type' => 'post',
'showposts' => 50 );

Because some bands feature less content than others, by the end of the loop I'm left with lots of one kind and only a couple of another so in order to balance the content I want to get to the end of my loop and ask something like:

if( number of posts in band 'learn' <= 6 ) {
    #load more learn posts
}

With this method I hope to get my most recent 50 posts but then ensure that I have a minimum of six in each band. I'm using Isotope to filter the Masonry layup so if the user hits the 'Learn' filter button I want to guarantee that the filtered results will find a minimum number of posts in any 'band'.

4
  • 1
    If I understand your question (which is questionable), you just need to iterate the returned bands and for each check whether they meet your condition... Is there something I'm missing? Can you show us the query your are executing prior to the pseudo code shown?
    – Dan
    Commented Mar 30, 2014 at 18:10
  • It is not clear what you are doing. Please edit the question to provide more detail.
    – s_ha_dum
    Commented Mar 30, 2014 at 18:14
  • Thanks for taking a look, I've just edited my question, I hope it makes more sense. Let me know if not. Commented Mar 30, 2014 at 18:30
  • Take a look at this
    – s_ha_dum
    Commented Mar 30, 2014 at 19:03

1 Answer 1

0

The literal implementation would be to:

  1. Create array variable
  2. While looping add to it ID of each post, using term name as key (so you have array of arrays)
  3. After main loop check the array and query additional posts if needed, using stored IDs for post__not_in so there are no duplicates

Syntax will be something like this:

$post_counts = array( 'learn' => array()  );

// append ID, $post_counts['learn'] will hold array of IDs
$post_counts['learn'][] = $post_id;

It might make sense however to rather loop through terms and retrieve six posts for each. It will not probably be significantly heavier computationally, but it will be simpler and easier to maintain logic.

1
  • Thanks @Rarst, that's definitely got me on the right track. Could you help me out with the array bit please? If I make an array like this: $post_count = array( array('learn'), array('trade'), array('invest'), array('spend') ); ?> How might I then shove the ID into it so that my output would resemble (where '123' is the ID): Array ( [0] => learn [0] => 1234 [1] => 1234 [2] => 1234 [3] => 1234 [1] => trade [0] => 1234 [1] => 1234 ... and so on? Commented Mar 31, 2014 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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