• Hi

    I have a custom post type for a jobs facility on the website. The closing date is meta data and should pull the job (post) from the website once that date has passed.

    Not a problem so far, my query does this well to display live jobs;

    $timecutoff = date("Ymd");
    			$args = array(
    			'post_type' => 'sc_jobs',
    			'orderby' => 'meta_value',
    			'meta_key' => 'closing_date',
    			'meta_compare' => '>=',
    			'meta_value' => $timecutoff,
    			'order' => 'ASC',
    			'paged' => $current_page,
    			'posts_per_page' => 5
    			);
    			$my_query = new WP_Query($args);

    For job categories, I set up the taxonomy ‘job_categories’.

    I want to list out the categories that feature live jobs, and display a post count next to the category

    $terms = get_terms('job_categories'); 
    if ( !empty( $terms ) && !is_wp_error( $terms ) ){ 
    echo '<ul>'; 
    foreach ( $terms as $term ) { 
    $term = sanitize_term( $term, 'job_categories' ); 
    $term_link = get_term_link( $term, 'job_categories' ); 
    echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '&nbsp;(' . $term->count . ')' . '</a></li>'; 
    } 
    echo '</ul>';
    }

    This however doesn’t take in to account my closing date meta, so the post count is incorrect.

    I’m stuck on this part, I thought this may be good but it results in nothing displayed

    $timecutoff = date("Ymd");
    $args = array(	'taxonomy' => 'job_categories',
    'orderby' => 'meta_value',
    'meta_key' => 'closing_date',
    'meta_compare' => '>=',
    'meta_value' => $timecutoff
    );
    $terms = get_terms($args);

    Can anyone help me query get_terms to include closing date?

    Thanks

    • This topic was modified 9 years, 2 months ago by candell.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Unless I’m mistaken, the closing date meta values are in the sc_jobs post meta. The query you are using is looking for meta values in term meta, not post meta. Even if you got terms related to open job listings, that would not affect the post count shown, it still reflects all jobs assigned the term, open or closed.

    I think the only solution is to loop through the terms, and for each term do a count query of open jobs assigned the current term. This would be fairly “expensive” computationally. It’d be a good idea to cache the results in a transient so those values can be easily retrieved as needed. The cache should be flushed anytime something is done to affect the counts, such as sc_jobs updates or new posts.

    I suppose another possibility would be to schedule a daily task to get all expired sc_jobs posts and strip out any assigned job category terms that are assigned. Then the post counts shown by default will reflect only open listings. As this is essentially destroying data, I’m not keen on this approach, but it is a possibility.

Viewing 1 replies (of 1 total)

The topic ‘meta query with get_terms’ is closed to new replies.