0

I would use together meta_query and date_query but it seems it's not possible .

What I'm looking to do : (display next 12 month from current month) : for each

  • April 2023

If date range event (start and end) is between 1st day of month and last day of month display only these events.

  • Mai

same thing

until March 2024

If month doesn't not have posts then display "No event this month". Considering the range of date event this one can be displayed on multiple month.

Currently : I have 4 active posts
My code display well events from start and end by month , but I can't do "else" condition to display "No event this month" because arguments of query are called for each month.

My reasoning was : if generated month ($months[$I]) correspond to one of date->month event (start and end) then display number of post for this month else display "No event this month" but I don't know how to get an array of start timestamp event to compare them to this generated month. Also "for" (generated month) loop it's done before wp loop.

Is it better to do one wp_query and then filter or do a loop wp_query for each month with specific arguments (for last one I can't do this) ?

$argslistyear = array(
            'post_type' => 'liste_associations',
            'post_status' => 'publish',
            'posts_per_page'   => -1,
            'meta_key' =>'start_timestampevents',
            'orderby' => 'meta_value_num',
            'order' => 'ASC',
            'meta_query' => array(
                'relation' => 'OR',
                array(
                    'key' => 'end_timestampevents',
                    'value' => array($datestart,$dateend),
                    'compare' => 'BETWEEN',
                    'type' => 'NUMERIC'
                ),
                array(
                    'key' => 'start_timestampevents',
                    'value' => array($datestart,$dateend),
                    'compare' => 'BETWEEN',
                    'type' => 'NUMERIC'
                )
            )
            /*
            ,'date_query' => array(
                array(
                    'after' => strtotime($FirstDayThisMonth[$i]), 
                    'before' => strtotime($LastDayThisMonth[$i]),
                    //'inclusive' => true,
                ),
            )
            */
        );
$postsListYear =  new WP_Query($argslistyear);

Currently my filter by month is :

add_action('wp_ajax_nopriv_filter_date', 'filter_ajax_date');
add_action('wp_ajax_filter_date', 'filter_ajax_date');

function filter_ajax_date(){
        

   if($postdaterange == 'listyear' )    :
        $months = array(); $FirstDayThisMonth = array(); $LastDayThisMonth =array();
        $currentMonth = (int)date('m');
        setlocale(LC_TIME, 'fr_FR');
        date_default_timezone_set('Europe/Paris');
        for ($x = $currentMonth; $x < $currentMonth + 12; $x++) {
            $months[] = date('F', mktime(0, 0, 0, $x, 1));
            $FirstDayThisMonth[] = date('Y-m-01 00:00:01', mktime(0, 0, 0, $x, 1));
            $LastDayThisMonth[] = date('Y-m-t 23:59:59', mktime(0, 0, 0, $x, 1));
        }
   
   for($i = 0; $i < 12; $i++) : // loop 12 months
        
        $monthinfrench = str_replace( $EnglishMth,$FrenchMth, date($FormatMth, strtotime($months[$i])));
    ?>

   <ListMonth>
            <span ><?= $monthinfrench; ?></span>

      if($postsListYear->have_posts()) : while ($postsListYear->have_posts()): $postsListYear->the_post(); // loop all posts 

         if($months[$i] == $TheStartMonth || $months[$i] == $TheEndMonth ) :


            elseif($months[$i] !== $TheStartMonth) : echo 'No event this month'; // error = this display 4 times -> 4 existing posts 
                //endif; // interval (1st day of month & start event) & (last day of month & end event )
            endif; // events of month       
        endwhile; //endwhile $post
    endif; //endif $post;
?>
    </ListMonth>
<?php
   endfor; //12 month
   wp_reset_query();
   wp_die();
endif; // Only Listyear
}// function filter_ajax_date
5
  • is this query inside a loop? Where does $i come from? What's the raw values/format of start_timestampevents and end_timestampevents in post meta? Does this work with the date_query uncommented and the meta_query commented out? How are you testing the result? Where do $datestart and $dateend come from and what are their formats/values? Please use the edit link under the tags to edit this information into your question as well as anything else that might be relevant to avoid additional questions, avoid posting code in comments
    – Tom J Nowell
    Commented Apr 29, 2023 at 16:04
  • Hi, @TomJNowell. without your questions I would not have thought of a variable problem but only a problem of arguments. My error are there : $FirstDayThisMonth[] = date('Y-m-01 00:00:01', mktime(0, 0, 0, $x, 1)); $LastDayThisMonth[] = date('Y-m-t 23:59:59', mktime(0, 0, 0, $x, 1)); instead of : $datestart = strtotime(date('Y-'.$nbofmonth.'-01 00:00:01')); $dateend = strtotime(date('Y-'.$nbofmonth.'-t 23:59:59')); It's always very annoying to have well written code and to realize that a small element breaks everything
    – imagIne
    Commented Apr 30, 2023 at 16:22
  • changing the server date and time via PHP functions such as date_default_timezone_set is very bad and messes with WordPress' internal functions. In WP all dates and times should be saved as UTC timestamps, not relative times in timezones. Is your last comment saying that you've found and fixed the error? If you have found the solution you should write it below in the answer box so that people know you've solved it, comments are just for clarifying questions
    – Tom J Nowell
    Commented Apr 30, 2023 at 23:21
  • This questions is resolved. @TomJNowell you'r right . I also discovered that strtotime display a false date using "-t" format to get the last day of month . I must use cal_days_in_month(CAL_GREGORIAN,nummonth, year);
    – imagIne
    Commented May 1, 2023 at 13:16
  • you need to write that as an answer, not a comment, there's a much larger box underneath the comments titled "Your Answer", the sites going to keep suggesting this question to people thinking you still need help, and nobody can upvote your solution so you don't get points
    – Tom J Nowell
    Commented May 2, 2023 at 8:30

1 Answer 1

0

Resolved. My error are there :

$FirstDayThisMonth[] = date('Y-m-01 00:00:01', mktime(0, 0, 0, $x, 1));           
$LastDayThisMonth[] = date('Y-m-t 23:59:59', mktime(0, 0, 0, $x, 1));

instead of :

$datestart = strtotime(date('Y-'.$nbofmonth.'-01 00:00:01'));   
$dateend = strtotime(date('Y-'.$nbofmonth.'-t 23:59:59')); 

It's always very annoying to have well written code and to realize that a small element breaks everything

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.