Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Filter out posts before display category count WordPress

  • SOLVED

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:

<?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++; } } ?>

Answers (3)

2013-12-31

Fahad Murtaza answers:

query_posts('meta_key=my-key-name&meta_value=' . $some_value);
echo $wp_query->found_posts;


So update the code to

echo $cat->count - $wp_query->found_posts;


Jagst3r21 comments:

How do I incorporate this into that?

$query->set(
'meta_query',
array(
array(
'key' => 'Due',
'value' => date( 'Ymd' ),
'type' => 'DATE',
'compare' => '>=', // greater than or equal to
)
)
);


Fahad Murtaza comments:

Hi

Did Hariprasad's code help. If not, I can still look into it.


Jagst3r21 comments:

I still need help his breaks my site.


Fahad Murtaza comments:

Above, in your code. get_the_category()

isn't there a syntax error right there. Can you paste the correct code that is working on your site?


Jagst3r21 comments:

This is what I currently have

<?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
query_posts('meta_key=Due-name&meta_value=' . $some_value);
echo '<a href="' . get_field('category_link', $post->ID) . '">' . '<div class="two">' . $cat->count . ' Scholarships' . '</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++; } } ?>


Jagst3r21 comments:

Any idea sir? This is kind of urgent


Fahad Murtaza comments:

OK

Here is the final answer


$counter = 0;

query_posts( array('post_type' => 'post',
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'date',
'cat'=> $cat->term_id,
'meta_query' =>
array(
array(
'key' => 'Due',
'value' => date( 'Ymd' ),
'type' => 'DATE',
'compare' => '>=', // greater than or equal to
)
)
));

if ( have_posts() ):
$counter=0;
while ( have_posts() ): the_post();
/*<span class="due"><strong>Due: </strong><?php echo date_i18n( get_option( 'date_format' ), strtotime( $due = get_post_meta( get_the_ID(), 'Due', true ) ) ); ?></span> &nbsp;&nbsp;*/
$counter++;
endwhile;
endif;

wp_reset_query();

2013-12-31

Hariprasad Vijayan answers:

Hello,

Try this code

<?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()
?>
<?php
$args = array(
'cat' => $cat->term_id,
'meta_query' => array(
array(
'key' => 'Due',
'value' => date( 'Ymd' ),
'type' => 'DATE',
'compare' => '>=', // greater than or equal to
)
)
);
$query = new WP_Query($args);
$postcount = $cat->count - $query->post_count;
?>
<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">' . $postcount . '</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++; } } ?>


Hariprasad Vijayan comments:

Is it working?


Hariprasad Vijayan comments:

Are you getting any error?


Hariprasad Vijayan comments:

I can check it. Could you provide your website details? Send PM

2014-01-01

Arnav Joy answers:

try this

<?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();

$args = array(

'cat' => $cat->term_id,

'meta_query' => array(

array(

'key' => 'Due',

'value' => date( 'Ymd' ),

'type' => 'DATE',

'compare' => '>='

)

)

);

$my_query = new WP_Query($args);

$postcount = $my_query->found_posts;



?>



<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">' . $postcount . '</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++; } } ?>