Filter out posts before display category count

I have meta key “Due” for posts….if a post has expired (past the current date or not equal to it) then it should not be displayed in the count (i have code to display the count, which fades in over a thumbnail)..for example right now one says it has 89, but it should really only be 26 because the rest have expired….so how do I filter out posts before the count is displayed?

Here is my code:

Read More
<?php
$categories = get_categories('hide_empty=0&exclude=13,1,1460&orderby=count&order=DESC&number=6');
if (!empty($categories)) {

    $i = 0;

    foreach ($categories as $cat) {
        $class = ( $i % 3 ) ? 'span4' : 'span4';

        $thumbnail_id = get_option('seamless_term_thumb_' . $cat->term_id);
        $image        = wp_get_attachment_url($thumbnail_id);

        get_the_category()

?>

        <div class="<?php echo $class . ' ' . 'category-' . $cat->term_id;?>">
            <div class="thumb one">
                <?php
                    echo '<a href="' . get_field('category_link', $post->ID) . '">' . '<div class="two">' . $cat->count . '</div>' . '</a>';
                ?>
                <a href="<?php echo get_field('category_link', $post->ID); ?>">
                    <img src="<?php echo $image; ?>" alt="<?php echo get_cat_name($cat->term_id); ?>" class="item-image">
                </a>
            </div>  <!-- end .thumb -->
        </div>

<?php $i++; } } ?>

Here is the new and updated code which works, but created A LOT of queries:

/**
 * Function to list all category with thumbnail custom link, etc..
 *
 * How to use this function:
 * Add in template: <?php my_category_list(); ?>
 *
 */
function my_category_list(){

/* LIST OF CATS DATA */
$cats_data = array();

/**
 * Get Categories
 * @link http://codex.wordpress.org/Function_Reference/get_categories
 */
$cat_args = array(
    'hide_empty'    => 0,
    'exclude'       => '13,1,1460'
);
$categories = get_categories( $cat_args );

/* If category found, load list of category */
if ( !empty( $categories ) ) {
    $i = 0;

    /* Foreach category: display the data */
    foreach ( $categories as $cat) {

        /* ======= HTML CLASS ========= */
        /* dynamic class (?) need fix */
        $class = ( $i % 3 ) ? 'span4' : 'span4';
        $classes = $class . ' ' . 'category-' . $cat->term_id;

        /* ======= POST COUNT ========= */
        /* Get all posts in category + in due date
         * this only to get the post count.
         * @link http://themehybrid.com/support/topic/issue-with-filtering-due-meta-key
         */
        $query_args = array(
            'post_type'       => 'post',
            'category_name'   => $cat->slug,
            'meta_query' => array(
                array(
                    'key'        => 'Due',
                    'value'      => date( 'Ymd' ),
                    'type'       => 'DATE',
                    'compare'    => '>=', // greater than or equal to
                )
            )
        );
        $my_query = new WP_Query( $query_args );
        $post_count = $my_query->found_posts;

        /* ====== CATEGORY THUMBNAIL ======== */
        $thumbnail_id = get_option('seamless_term_thumb_' . $cat->term_id);
        $image = wp_get_attachment_url($thumbnail_id);

        /* ====== LINK TO SEARCH: no need fields ======= */
        $link_to = 'http://www.scholarships360.org/discover/?search_query=&orderby=blank&tax_category=' . $cat->slug .'&wpas=1';

        /* MERGE DATA IN ARRAY */
        $cats_data[] = array(
            'classes'      => $classes,
            'post_count'   => $post_count,
            'image'        => $image,
            'name'         => $cat->name,
            'link'         => $link_to,
        );

        $i++;
    } // end foreach

    /**
     * NOW THE FUN PART
     * =================
     */

    /* Sort Cat Data by Post Count */
    usort($cats_data, 'my_sort_cat_data');

    /* Cut only 6 item to display */
    $cats_data = array_slice( $cats_data, 0, 6 );

    /* Display it */
    foreach ($cats_data as $cat_data ){ ?>

        <div class="<?php echo $cat_data['classes'];?>">
            <div class="thumb one">
                <a href="<?php echo $cat_data['link'] ?>">
                    <div class="two"><?php echo  $cat_data['post_count'] . ' Scholarships' ?></div>
                </a>
                <a href="<?php echo $cat_data['link'] ?>">
                    <img src="<?php echo $cat_data['image']; ?>" alt="<?php echo esc_attr( $cat_data['name'] ); ?>" class="item-image">
                </a>
            </div>  <!-- end .thumb -->
        </div>
    <?php 
    }
}
/* No category found */
else {
    echo '<p>No category found...</p>';
}
}

 /**
 * Sort Cat Data Helper Function
 * @link http://stackoverflow.com/questions/2699086/sort-multidimensional-array-by-value-2
 */
function my_sort_cat_data( $a, $b ){
 return $b['post_count'] - $a['post_count'];
}

Related posts

1 comment

  1. As pointed out by @Jagst3r15, you’re going to have to loop through each category’s posts no matter what to filter out those posts that hold your meta key.

    For a high-level answer: I’d suggest looking at the INNER JOIN logic that happens in _pad_term_counts() for an example of how to bring in the posts table. Then filter on the get_terms hook to loop through those results and exclude what you don’t need.

    It’s a good question with a not-very-simple answer, unfortunately.

Comments are closed.