I’m making a magazine theme for a client and using a custom ‘issue’ taxonomy. There is a new issue every week, and I also have a function that get’s the latest issue. But the editor creates a new issue a few days before the articles for that issue are published. My current function uses max() to just find the highest numbered issue:

function get_issues() {
    $output = array();
    $hlterms = get_terms('issue', array('hide_empty' => false));
    foreach($hlterms as $term){
         array_push($output, $term->name);
    } 
    return $output;
}
function get_current_issue() {
    $current_issue_no = max(get_issues());
    $current_issue = get_issue_by_number($current_issue_no); // Not important for question
    return $current_issue;
}

All the terms in ‘issue’ are numbers (as strings) like 101,402, etc. I use the highest number to return an Issue object, but that’s not really important here, because if I can get the correct number I can then just pass that to the already working instantiation function get_issue_by_number().

So how can I find out whether that ‘issue’ that is returned has published posts? The idea is that the get_current_issue() function would only return the latest issue that is considered active by the publisher. They create articles beforehand and give them a publish date a few days from upload time. So ideally I could write a function that would return issue 101 even if 102 exists because 102 isn’t assigned to any active posts. But then once those posts became active on the set date the function would begin returning 102. All thoughts are welcome, happy to change my approach if anyone has a better way to do this.

So, a quick example of what I would hope to accomplish:

I have two ‘issues’, 123 and 124. I created 123 a while ago and have since assigned published posts to it. But I just recently created 124 and there are no published posts attached yet. My function should return issue 123 not 124, even though 124 is technically more recent. The fact that issue 123 has a number of published posts assigned, while 124 has none would determine this result. Then once I publish a post assigned to issue 124 it would become the current issue.

3 Answers
3

function get_issues() {
 $output = array();
 $hlterms = get_terms('issue', array('orderby' => 'id', 'order' => 'DESC','hide_empty' => false));
 foreach($hlterms as $term){
     array_push($output, $term->term_id);
 } 
 return $output;
}

This would return you term id as per the recent and then in your second function start iterating to get the posts for latest issue.

function get_posts_for_current_issue() {
   $total_issues = get_issues();
   foreach($total_issues as $issue_id){
     $args = array(
     'post_type' => 'post',
         'status' => 'publish',
     'tax_query' => array(
          array(
        'taxonomy' => 'issue',
        'field' => 'id',
        'terms' => $issue_id
          )
           )
        );//end of args
     $current_issue_posts = get_posts($args);
     if(!is_wp_error($current_issue_posts) && count($current_issue_posts)>0){
        return $current_issue_posts; //will terminate the loop if posts found
     }
   }//end of foreach
 }//end of function

You might need to turn on the WP_DEBUG, as I can’t test the code.

Leave a Reply

Your email address will not be published. Required fields are marked *