3

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.

7
  • So suppose if there are issues like 303, 404, 101, 102 and out of the four the two of them "303" and "404" have posts, what in that case? What the post assigned as "303" is supposed to do, does it describr the issue or what? A little more description would be helpful
    – Kumar
    Commented Aug 14, 2013 at 12:45
  • Sure, in that case I would want my function to return 404. I use the max() function because it seemd like an easy method, but it doesn't account for whether or not this term is assigned to any published posts.
    – ian
    Commented Aug 14, 2013 at 13:25
  • You want 404 thats fine but as I said both 303 and 404 have a post so why you want the greater one, you are not making any sense
    – Kumar
    Commented Aug 14, 2013 at 13:27
  • I would want the greater one because these are chronologically published, so 404 is more recent than 303. it is only if I don't have any published posts assigned to 404 that I would want an older issue returned.
    – ian
    Commented Aug 14, 2013 at 13:31
  • Ohhh , I confused your issue with errors so the issue 404, 405, 406, 407 is just a simple issue number? Also issue is being confused here with problem :) You could rather prefer edition to avoid the same confusion
    – Kumar
    Commented Aug 14, 2013 at 13:37

3 Answers 3

1
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.

3
  • Thanks for the answer dot, I haven't had a chance to run it yet but I'll post the results when I do.
    – ian
    Commented Aug 14, 2013 at 17:14
  • Nice. This just about did it for me. I ended up using the 'name' instead of 'id', but it's pretty much the same function. Also, there is a typo in your if statement at the second $current_issue_posts. Missing one r. Thanks for the help
    – ian
    Commented Aug 15, 2013 at 15:51
  • great :), I just wrote it directly here without even testing thats y a typo :), glad it worked for you :)
    – Kumar
    Commented Aug 15, 2013 at 18:23
2

I'd suggest using 'field'=>'term_id' instead of 'field'=>'id'. Regarding wordpress codex page for get_terms() it would be the right way and also that was my problem during testing.

My code example (to check if term has published posts):

function check_term_posts($tax_slug, $term_id) {
    $args = array(
        'post_type' => 'post',
        'status' => 'publish',
        'tax_query' => array(
            array(
                'taxonomy' => $tax_slug,
                'field' => 'term_id',
                'terms' => $term_id
            )
        )
    );
    $term_query =  new WP_Query($args);
    $term_posts_count = $term_query->found_posts;
    if( $term_posts_count>0 ){
        return true;
    } else {
        return false;
    }
}
0
if (get_term($term->term_id, 'taxonomy')->count > 0 ) {

}
3
  • 2
    Please edit your answer, and add an explanation: why could that solve the problem?
    – fuxia
    Commented Dec 24, 2017 at 0:07
  • Do not use this, WP has a helper to check if term exists: term_exists($term_id, 'taxonomy').
    – Bjorn
    Commented Jul 22, 2019 at 1:28
  • 1
    "exist" and "not empty" - different things.
    – Flector
    Commented Jul 23, 2019 at 10:42

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.