wp-graphql-meta-query icon indicating copy to clipboard operation
wp-graphql-meta-query copied to clipboard

Cannot get the metaQuery to run multiple conditionals. There's a bug with map_input_fields.

Open ytwguru opened this issue 6 years ago • 4 comments

Probably due to a change in the version, but queries using the metaArray don't work correctly when querying a large conditional statement.

I inspected the meta_query generated and it looks like the code below seems to be causing the issue. (It can be deleted and the function can be modified)

foreach ( $meta_query['metaArray'] as $meta_query_key => $value ) {
   $meta_query[] = [
   $meta_query_key => $value,
   ];
}

The output below is from of the meta_query generated from the current (unchanged code)


[meta_query] => Array  (     
     [0] => Array  (     
         [0] => Array  (           
             [key] => event_dates_0_start_time
             [value] => 2019-08-28 00:00:00
             [compare] => >=  
             [type] => DATETIME
         )               
     )
     [1] => Array  (
         [1] => Array  (
             [key] => event_dates_1_start_time
             [value] => 2019-08-28 00:00:00
             [compare] => >=   
             [type] => DATETIME
         ) 
      )
      [2] => Array  (
          [2] => Array  (
              [key] => event_dates_2_start_time
              [value] => 2019-08-28 00:00:00
              [compare] => >= 
              [type] => DATETIME
         )
      )
  )

ytwguru avatar Aug 29 '19 01:08 ytwguru

@ytwguru can you provide an example of the GraphQL query you were trying to make when you noticed this so that I can replicate?

jasonbahl avatar Aug 29 '19 02:08 jasonbahl

For context.

  • I Have a custom post type - events.
  • I created an ACF field called event_dates using an ACF repeater
  • The repeater has subfields start_time and end_time

Here's the query.

query MyQuery {
  events(
    first: 5, 
    where: {
      metaQuery: {
        relation: OR,
        metaArray: [{
          compare: GREATER_THAN_OR_EQUAL_TO,
          type: DATETIME,
          value: "2019-08-28 00:00:00",
          key: "event_dates_0_start_time"
        }, {
          compare: GREATER_THAN_OR_EQUAL_TO,
          type: DATETIME,
          value: "2019-08-28 00:00:00",
          key: "event_dates_1_start_time"
        }, {
          compare: GREATER_THAN_OR_EQUAL_TO,
          type: DATETIME,
          value: "2019-08-28 00:00:00",
          key: "event_dates_2_start_time"
        }, {
          compare: GREATER_THAN_OR_EQUAL_TO,
          type: DATETIME,
          value: "2019-08-28 00:00:00",
          key: "event_dates_3_start_time"
        }, {
          compare: GREATER_THAN_OR_EQUAL_TO, 
          type: DATETIME, 
          value: "2019-08-28 00:00:00", 
          key: "event_dates_4_start_time"
        }]
      }, 
      orderby: {
        field: DATE, 
        order: ASC
      }
    }
  ) {
    edges {
      node {
        title
        link
      }
    }
  }
}

The query should list current events.
As an aside, I would like to order the results by the start_time if possible (requested feature)

ytwguru avatar Aug 29 '19 07:08 ytwguru

Same Issue here, with this kind of query :

metaQuery: {
        relation: 'OR,
        metaArray:  [
           {
             key: 'post_subject',
             value: '428',
             compare: 'LIKE',
             type: 'CHAR',
           },
           {
             key: 'post_subject',
             value: '392',
             compare: 'LIKE',
             type: 'CHAR',
           },
           {
             key: 'post_subject',
             value: '352',
             compare: 'LIKE',
             type: 'CHAR',
           },
         ],
}

Where post_subject is a ACF relationship field. The query works fine if I execute it the usual wordpress way. It also works If I execute it with one or 2 conditionals.

I'm using wpgraphql 1.5.9 because of this other issue.

LucasDemea avatar Apr 27 '23 16:04 LucasDemea

It works again if I remove these lines :

if ( 2 < count( $meta_query['metaArray'] ) ) {
	unset( $meta_query['relation'] );
}

LucasDemea avatar Apr 27 '23 17:04 LucasDemea