Query ID and Meta Query Parameters Questions (solved)

I am trying to create a custom query for a loop. I have a CPT Sales and two of the fields are promotion_start and promotion_end, which are ACF Date Picker fields. I have a page that lists sales based on if the current date is <= promotion_start and >= promotion_end. A few questions, if anyone knows:

-I understand I will need to use a custom query filter and it requires the query ID. On the block inspector I see a query class but not a query ID. When I look at the query editor I see it says Query ID 18. Is that the query ID I use, such that the query ID in the filter would be:

$id === 'query-18'

-This is the query filter I’m trying. Since I have two Meta Queries, I added two values. I assume that is needed.

<?php
add_filter( 'cwicly/query/args', function( $query_args, $attributes, $id ) {
    if ($id === 'query-18') {
        $query_args['meta_query'][0]['value'] = date('Y-m-d'); 
        $query_args['meta_query'][1]['value'] = date('Y-m-d'); 
    }
    return $query_args;
}, 10, 3 );
?>

-These are my Meta Queries - The start screen:
meta-query-start
The first Meta Query:


The second Meta Query:

-Instead of filtering the items, all are returned. I looked at the code and see that the Keys in the Meta Queries are Null.

<?php 
// The Arguments
$args = array (
  'post_type' => 
  array (
    0 => 'on-sale',
  ),
  'ignore_sticky_posts' => true,
  'meta_query' => 
  array (
    'relation' => 'AND',
    0 => 
    array (
      'key' => NULL,
      'compare' => '<=',
      'type' => 'DATE',
      'value' => 
      array (
      ),
    ),
    1 => 
    array (
      'key' => NULL,
      'compare' => '>=',
      'type' => 'DATE',
      'value' => 
      array (
      ),
    ),
  ),
  'orderby' => 'title',
  'order' => 'ASC',
);
// The Query
$the_query = new WP_Query( $args );

I’ve pieced this approach together by looking at other posts in the forum here, so I probably misunderstood or missed a step. Any help is appreciated.

That’s a great request!
I am also particularly interested in implementing this filter between ACF dates.

Hey @David ,

i think you are targeting the wrong “ID”. It’s not the ID inside the query modal but the real HTML ID in the right sidebar which you have to use.

(If you only see the class, then you have to click on the word “class” to toggle between ID and class)

The rest looks ok on first sight!

Cheers
Wolfgang

1 Like

Thanks for helping out here, @Wolfgang.

Indeed, as specified in the docs, the reference is the Query Block ID as Query IDs are only relative to their current page.

@Wolfgang - Thank you very much. I wasn’t aware that clicking on the word “class” would toggle to ID.

I got the query ID as @Wolfgang suggested and nothing showed after I put that in the code filter. That was a change as previously everything showed. In experimenting around in the Meta Queries I deleted the key by clicking on the orange database icon and when I did so I saw “field_645eaeb602170” instead of the field name that showed in the drop down. I removed that and manually typed in the field names of the keys (‘promotion_state’ and ‘promotion_end’) into the two Meta Queries and everything worked as expected. I’m sharing this in case others have a similar experience.