• Resolved mtm

    (@flatpack-music)


    My site uses a CPT called ‘event’. Some events are multi-day, some are single day. The query I have written to display them in a list checks to see if a end date has been added before using the start date as a filter to check whether the event should be displayed as Upcoming or Past.

    I need to deploy Ajax Load More in the ‘Past Events’ list to avoid it being huge on load. How can i create a query in the shortcode that first checks whether we have a single- or multi-day event on our hands.

    Here’s my query:

    <?php 
    date_default_timezone_set('Europe/London'); 
    $date_1 = date('Ymd', strtotime("-999 months"));
    $date_2 = date('Ymd', strtotime("now"));
    ?>
    <?php
    $past_args = array(
     'post_type' => 'event',
     'meta_query' => array(
      'relation' => 'OR',
       // check to see if end date has been set
       array(
        'key' => 'end_date',
        'compare' => 'BETWEEN',
        'type' => 'DATE',
        'value' => array($date_1, $date_2),
       ),
       // if no end date has been set use event/start date
       array(
        'key' => 'start_date',
        'compare' => 'BETWEEN',
        'type' => 'DATE',
        'value' => array($date_1, $date_2),
       )
      ),
      'orderby' => 'meta_value_num',
      'order' => 'DESC',
      'nopaging' => true
     );
    ?>

    The result here is:

    1. Events which have an end_date are only included if their end_date has passed. Other events are included if their start_date has passed.
    2. Events are ordered by start_date regardless of whether they have an end_date or not.

    Is this possible? I’ve managed something similar in the past but only for situations in which all events have only one date:

    <?php echo do_shortcode('[ajax_load_more post_type="event" posts_per_page="12" meta_key="event_date" meta_value="'. $today .'" meta_compare="<" meta_type="DATE" orderby="meta_value_num" offset="12" pause="true" scroll="false" button_label="Show More"]'); ?>

    It would be great to recreate this whilst checking for an end_date value first.

    Thank you.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Darren Cooney

    (@dcooney)

    @flatpack-music
    Yes, you can do that.

    I’m going to simply your code sample into a shortcode only. Using a $start and $end variable date, you can use the BETWEEN compare operator.

    <?php echo do_shortcode('[ajax_load_more post_type="event" posts_per_page="12" meta_key="event_date" meta_value="'. $start .','. $end .'" meta_compare="BETWEEN" meta_type="DATE" orderby="meta_value_num" offset="12" pause="true" scroll="false" button_label="Show More"]'); ?>

    Let me know if this helps at all.

    Thread Starter mtm

    (@flatpack-music)

    Thanks so much for getting back to me. I’m not sure what you’ve created here quite does what I’m trying to though. I think I confused things by including a previous example which uses different keys.

    So – forget the event_date key. Here we are using a combination of start_date and end_date.

    If you look again at the initial block of code in my first post, you see that there are TWO variables: the two dates which make up the BETWEEN operator and the two possible keys to pass through that operator.

    The process that first query creates is:

    1. Find posts of type event.
    2. Check if post has an end_date value. If so, use it as the key.
    3. If post no end_date value use the start_date value (there is always a start_date value).
    4. Compare the key to the meta value (using BETWEEN on the date range as in your shortcode).
    5. Include post IF the key (which, from post to post, could be either end_date or start_date) falls between the meta values.
    6. Your shortcode creates the date range in which the key needs to fall, but it uses one key – event_date – whereas I need it to check for end_date before using start_date.

      I hope that is clearer – apologies for the second bit of code in my original post, which I suspect has made this confusing.

      Thanks.

    • This reply was modified 4 years, 1 month ago by mtm.
    • This reply was modified 4 years, 1 month ago by mtm. Reason: Corrections and formatting to improve clarity
    Plugin Author Darren Cooney

    (@dcooney)

    @flatpack-music If you can create a Meta_Query that works for this then you can use it in Ajax Load More.
    You can use the alm_query_args filter to essentially build your own WP_Query.
    https://connekthq.com/plugins/ajax-load-more/docs/filter-hooks/#alm_query_args

    Hope this helps.

    Thread Starter mtm

    (@flatpack-music)

    Oh this looks very promising. I had a go but so far no dice – you can see what’s happening at the staging site.

    Here’s what I’ve added to the functions.php file:

    
    // ALM Shortcode
    // [ajax_load_more id="past_event_listing" posts_per_page="8" offset="8" button_label="Show More"]
    // 'past_event_listing' is the value of the 'id' parameter in the shortcode.
    
    date_default_timezone_set('Europe/London'); 
    $alm_date_1 = date('Ymd', strtotime("-999 months"));
    $alm_date_2 = date('Ymd', strtotime("now"));
    
    $alm_past_args = array(
     'post_type'			=> 'event',
     'meta_query'		=> array(
      'relation'	=> 'OR',
    // check to see if end date has been set
      array(
       'key' => 'end_date',
       'compare' => 'BETWEEN',
       'type' => 'DATE',
       'value' => array($alm_date_1, $alm_date_2),
      ),
     // if no end date has been set use event/start date
      array(
       'key' => 'start_date',
       'compare' => 'BETWEEN',
       'type' => 'DATE',
       'value' => array($alm_date_1, $alm_date_2),
      )
     ),
     'orderby' => 'meta_value_num',
     'order' => 'DESC',
    );
    return $alm_past_args;
    
    }
    add_filter( 'alm_query_args_past_event_listing', 'my_past_event_listing', 10, 2);
    

    Here’s my shortcode:

    [ajax_load_more id="past_event_listing" posts_per_page="8" offset="8" button_label="Show More"]

    And here’s what the ALM container returns in the inspector:

    
    <ul aria-live="polite" aria-atomic="true" class="alm-listing alm-ajax eventlist nopad nostyle" data-container-type="ul" data-repeater="default" data-post-type="post" data-order="DESC" data-orderby="date" data-offset="8" data-posts-per-page="8" data-scroll="true" data-scroll-distance="100" data-max-pages="0" data-pause-override="false" data-pause="false" data-button-label="Show More"></ul>
    

    Any idea why it appears to be returning nothing? The same query in a template works, and there should be a further 15 events to list here.

    Thanks.

    Thread Starter mtm

    (@flatpack-music)

    I’d really appreciate your thoughts on this one? So far as I can see I’m doing what I’m supposed to – I’ve obviously missed some key step. Thanks.

    Plugin Author Darren Cooney

    (@dcooney)

    If you provide a working WP_Query I can look and convert it for use with ALM.

    Thread Starter mtm

    (@flatpack-music)

    Thanks – I thought I had done this. The query in my original post (or indeed the one from Nov 2nd) does work. You can see it in action at the staging site, under Past Events here. I’ve limited this query to just the first 8 posts. I intend to use an offset of in the eventual ALM shortcode to load subsequent posts.

    If I’ve missed something here and by “a working WP_Query” you mean somethign different could you let me know please? Obviously I’d like to do all I can to allow you to help me here.

    Thanks again.

    Plugin Author Darren Cooney

    (@dcooney)

    Your code sample on November 2nd is using the incorrect syntax for the this filter and you’re not returning the $args object either. Why are you not following the example code?
    https://connekthq.com/plugins/ajax-load-more/docs/filter-hooks/#alm_query_args

    function my_past_event_listing($args, $id){
    
    	date_default_timezone_set('Europe/London'); 
    	$alm_date_1 = date('Ymd', strtotime("-999 months"));
    	$alm_date_2 = date('Ymd', strtotime("now"));
    	
    	$args['post_type'] = 'event';
    	$args['meta_query'] = array(
    		'relation' => 'OR',
    		array(
    			'key' => 'end_date',
    			'compare' => 'BETWEEN',
    			'type' => 'DATE',
    			'value' => array($alm_date_1, $alm_date_2),
    		),
    		array(
    			'key' => 'start_date',
    			'compare' => 'BETWEEN',
    			'type' => 'DATE',
    			'value' => array($alm_date_1, $alm_date_2),
    		)
    	);
    
    	$args['orderby'] = 'meta_value_num';
    	$args['order'] = 'DESC';
    
    	return $args;
     
    }
    add_filter( 'alm_query_args_past_event_listing', 'my_past_event_listing', 10, 2);
    Thread Starter mtm

    (@flatpack-music)

    Yup that does it – thank you very much for the help. I thought I was following the example code (in the 2nd version above) but I didn’t realise the syntax needed to be adjusted to build the $args quite like that.

    Anyway, this has worked perfectly. Again, really appreciate you putting in the time to help us use this plugin well. Thanks!

    Hi Darren,

    Is it possible to write the query (or in general a nested meta query) in a shortcode like

    echo do_shortcode( '[ajax_load_more … ]' );

    Thanks!

    • This reply was modified 3 years, 11 months ago by Chris.
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Ajax Load More with nested meta_query’ is closed to new replies.