Rwhy
Rwhy

Reputation: 25

Sorting graphql query by acf field - React Native / Wordpress

working on a graphQl, react Native , Wordpress backend Project & struggle to find an answer. I got a query and i like to sort (or order) by an ACF field.

I'm quite new on graphQl so i don't know where to look or what yo search (if specific)

Here my query:

    const queryAllEvent = <Query query={gql`
{posts(where: {categoryNotIn: "12"}) {
    edges {
      node {
        id
        featuredImage {
          node {
            sourceUrl
          }
        }
        title
        prix {
          prix
        }
        content
        date_event {
            time
            dateEvent
          }
        
      }
    }
  }
}
`}

I like to sort it (or order as i said) by dateEvent.

Any clue?

Not specially looking for a proper answer, but where to search. Sorry for my english.

Thanks

Upvotes: 1

Views: 1333

Answers (1)

Rwhy
Rwhy

Reputation: 25

Ok, finally found out after a lonnnng search ! Here the answer, for the one, who'd like to see !

First of all, as javascript and php date are count in millisecond & second, need to convert the date picker acf to correct value.

/**
 * Convert values of ACF core date time pickers from Y-m-d H:i:s to timestamp
 * @param  string $value   unmodified value
 * @param  int    $post_id post ID
 * @param  object $field   field object
 * @return string          modified value
 */
function acf_save_as_timestamp( $value, $post_id, $field  ) {
    if( $value ) {
        $value = strtotime( $value*1000 );
    }

    return $value;    
}

add_filter( 'acf/update_value/type=date_time_picker', 'acf_save_as_timestamp', 10, 3 );

/**
 * Convert values of ACF core date time pickers from timestamp to Y-m-d H:i:s
 * @param  string $value   unmodified value
 * @param  int    $post_id post ID
 * @param  object $field   field object
 * @return string          modified value
 */
function acf_load_as_timestamp( $value, $post_id, $field  ) {
    if( $value ) {
        $value = date( 'Y-m-d H:i:s', $value*1000 );
    }

    return $value;    
}

add_filter( 'acf/load_value/type=date_time_picker', 'acf_load_as_timestamp', 10, 3 );

After that need to add your field to graphql_PostObjectsConnectionOrderbyEnum_values & graphql_post_object_connection_query_args:

add_filter( 'graphql_PostObjectsConnectionOrderbyEnum_values', function( $values ) {

    $values['STARTEVENT'] = [
        'value' => 'startevent',
        'description' => __( 'The date of startevent on the post', 'wp-graphql' ),
    ];

    return $values;

} );

add_filter( 'graphql_post_object_connection_query_args', function( $query_args, $source, $input ) {

    if ( isset( $input['where']['orderby'] ) && is_array( $input['where']['orderby'] ) ) {

        foreach( $input['where']['orderby'] as $orderby ) {

            if ( ! isset( $orderby['field'] ) || 'startevent' !== $orderby['field'] ) {
                continue;
            }

            $query_args['meta_key'] = 'startevent';
            $query_args['orderby'] = 'meta_value_num';
            $query_args['order'] = $orderby['order'];

        }

    }

    return $query_args;

}, 10, 3);

Finally, your field will appear on the orderby (my field is the startevent)

Capture of result

Upvotes: 1

Related Questions