0

I have 2 custom fields associated with each post in Y-m-d format: start_date being the start date of the post and end_date being the ending date.

I can easily display upcoming post, current post and past post, but I cannot get the meta_query right to display total post count of upcoming post or current post or past post (with a start date less than or equal to today AND end date greater than or equal to today). The code below displays upcoming post :

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$today = date('Y-m-d', strtotime('-1 hours'));
query_posts(array(
'post_type' => 'post', 
'paged' => $paged,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query'=>array(
    'relation'=>'AND',
    array(
        'key' => 'start_date',
        'value' => $today,
        'compare' => '<=',
        'type' => 'CHAR'
        ),
    array(
        'key' => 'end_date',
        'value' => $today,
        'compare' => '>=',
        'type' => 'CHAR'
        )
    )
));
if (have_posts()) :
while (have_posts()) : the_post();

with this code i am able to display current post with date range but i want to print total post count.

please help ?

0

If you save the return of query_post(...) as a variable, and then run $var->found_posts, does that give you the expected result?

Like so:

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$today = date('Y-m-d', strtotime('-1 hours'));
$query = query_posts(array(
'post_type' => 'post', 
'paged' => $paged,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query'=>array(
    'relation'=>'AND',
    array(
        'key' => 'start_date',
        'value' => $today,
        'compare' => '<=',
        'type' => 'CHAR'
        ),
    array(
        'key' => 'end_date',
        'value' => $today,
        'compare' => '>=',
        'type' => 'CHAR'
        )
    )
));
echo $query->found_posts . " posts.";
if (have_posts()) :
while (have_posts()) : the_post();

If you adjust the compare-value in the query you should get the according result?!

  • Vincens von Bibra thank you for replying. i tried this but i am getting no total count value. please help ? – gul rathod Apr 21 '18 at 5:45
  • What do you get? What does the line echo $query->found_posts . " posts."; print out? – Vincens von Bibra Apr 21 '18 at 13:38
  • its only print "posts." – gul rathod Apr 22 '18 at 17:16
  • And while (have_posts()) : the_post(); also prints nothing? – Vincens von Bibra Apr 23 '18 at 5:44
  • 1
    hey Vincens von Bibra! first of all sorry for late reply, i was bizy with some other things, today i have checked the code and its working. thanks for your support. its working fine. i have one other query which i have post today. link : wordpress.stackexchange.com/questions/303257/… can you please help me to solve this ? thanks :) – gul rathod May 10 '18 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.