• Resolved Nugerama

    (@nugerama)


    Hi there. I’m really hoping that I can use this plugin for a specific scenario – this might constitute a feature request though: I want to output a query loop of events. Actually I want to output two query loops: one for forthcoming events and below that another one for past events. Currently I achieve this with a pre_get_posts filter appending a meta query like:

    ‘key’ => ‘event_end’,
    ‘compare’ => ‘>’,
    ‘value’ => date(‘Y-m-d’),
    ‘type’ => ‘DATE’

    However, it appears only possible to use the meta query with a static value for comparison. Any pointers? Many thanks.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Lane

    (@laned69)

    Here is my workaround.

    I typed the word “todays_date” in the Meta Value field in the Post Meta Query section of the AQL block settings. Then I added the following code in my theme’s functions.php file. It basically looks at what has been typed in, and if it is “todays_date”, then it changes it to the numerical date by using the date() function.

    function change_query_date_value( $query_args, $block_query, $inherited ) {
    	if ( isset( $query_args['meta_query'] ) && ! empty( $query_args['meta_query'] ) ) {
    		
    		if ( $query_args['meta_query'][0]['value'] == 'todays_date' ) { $query_args['meta_query'][0]['value'] = date('Ymd'); }
    	
    	}
    	return $query_args;
    }
    add_filter( 'aql_query_vars', 'change_query_date_value', 10, 3 );
    Thread Starter Nugerama

    (@nugerama)

    Thanks, @laned69, that’s a great solution. However, I’ve actually gone ahead and created a block variation with a pre_render_block and query_loop_block_query_vars filter combo which has enabled me to tap into the query vars as required. I’ll keep your method in mind though for future applications.

    Carl

    (@carlbtmn)

    @laned69 Can you help me configure the slotfills? I have no idea where to start like what files do I need to place before I create the custom plugin? The instructions in the Github are horrible. Any help?

    @nugerama Sorry to burst in like that, but out of curiosity, and because I’ll need this (ACF-based event dates filtered loop queries) for several sites as well: did you create a block variation of the standard Loop Query Block or of Advanced Loop Query? Seems like a much cleaner solution than having to add PHP filters, at least until @welcher adds this functionality to Advanced Query Block itself (have to admit I landed here because I couldn’t understand why meta_value “today” didn’t work out of the box – I’m probably spoiled from WP GridBuilder).
    Thanks!

    Thread Starter Nugerama

    (@nugerama)

    Hi, @philibee. I created block variations of the core Query Loop block. Here’s an example of how I targeted the ‘past-events’ variation in order to manipulate the query:

    function my_namespace_upcoming_events_pre_render_block( $pre_render, $parsed_block, $parent_block ) {
    
      // target the query loop block variations
      if( isset( $parsed_block[ 'attrs' ][ 'namespace' ] ) ):
    
        if( 'my_namespace/past-events' === $parsed_block[ 'attrs' ][ 'namespace' ] ):
    
          add_filter( 'query_loop_block_query_vars', 'my_namespace_query_loop_block_query_vars_past_events', 10, 1 );
    
        endif;
    
      endif;
    }
    add_filter( 'pre_render_block', 'my_namespace_upcoming_events_pre_render_block', 10, 3 );
    
    function my_namespace_query_loop_block_query_vars_past_events( $query ) {
    
      $query[ 'meta_query' ] = [[
    
        'key'     => 'event_end', // my ACF datetime field
        'compare' => '<=',
        'value'   => date('Y-m-d H:i:s'),
        'type'    => 'DATETIME'
      ]];
    
      $query[ 'order' ] = 'DESC';
      $query[ 'orderby' ] = 'meta_value';
      $query[ 'meta_key' ] = 'event_start';
      $query[ 'meta_type' ] = 'DATETIME';
    
      remove_filter( 'query_loop_block_query_vars', 'my_namespace_query_loop_block_query_vars_past_events', 10, 1 );
    
      return $query;
    }

    Targeting pagination was a whole other inexact science that I achieved by adding some parent classes in order to target child-pagination blocks. Also I didn’t get so far as reflecting this query manipulation in the block editor, only on the front end.

    I hope that’s helpful.

    • This reply was modified 8 months, 1 week ago by Nugerama.

    Hey @nugerama
    really sorry for the noreply – somehow missed your post!
    In the end I went with @laned69 ‘s code (adapted slightly to be able to display “current event” with two meta compares, start and end date), as I’m in a bit of a hurry.
    As soon as things calm down a bit, I’ll gladly look into creating and using block variations, as per your code – thanks a lot for that!
    Cheers, Phil

    Thread Starter Nugerama

    (@nugerama)

    No worries, @philbee. Glad you got it sorted.

    Thank you @laned69 !
    I am using your solution with META VALUE “todays_date” and META COMPARE >= to get all coming events since today. With <= I am getting all past events, clearly.
    Do you have an idea how to toggle between the two, so that the user can click somewhere to do this action?

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.