0

I use getEntityRecords as described in the official data API tutorial (https://learn.wordpress.org/lesson/retrieving-wordpress-data-records/).

I.e.:

    const [searchTerm, setSearchTerm] = useState( '' );
    const pages = useSelect( select => {
        const query = {};
        if ( searchTerm ) {
            query.search = searchTerm;
        }
        return select( coreDataStore ).getEntityRecords( 'postType', 'myCustomPostType', query );
    }, [searchTerm] );

This works fine and as expected. Now I want to adjust the query so that the posts' meta can be used to reduce the query result.

I tried various approaches such as

const [searchTerm, setSearchTerm] = useState( '' );
const pages = useSelect( select => {
    const query = {};
    if ( searchTerm ) {
        query.search = searchTerm;
    }
    if (postId) {
      query.meta = [
        {
        myMetaKey: postId,
        },
        ];
     }
    return select( coreDataStore ).getEntityRecords( 'postType', 'myCustomPostType', query ); 
}, [searchTerm] );

but to no avail.

As I understand the 'getEntityRecords' documentation:

query GetRecordsHttpQuery: Optional terms query. If requesting specific fields, fields must always include the ID. For valid query parameters see the Reference in the REST API Handbook and select the entity kind. Then see the arguments available for “List [Entity kind]s”.

querying the metadata should be possible.

I'm aware of meta_query with standard getEntityRecords possible? but am not fully sure if it relates to this problem.

Any help appreciated.

1 Answer 1

1

Ok, I found the answer myself thanks to Can you filter posts by meta key with the getEntitityRecords selector?

Despite my first understanding the >>>query<<< parameters for list posts given in the Reference do >>>not<<< contain the meta.

I followed the article and created a query filter for the CPT, i.e.

    function mycustomposttype_meta_query_filter()
    {
      add_filter(
        'rest_mycustomposttype_query',
        function ($args, $request) {
          if ($meta_key = $request->get_param('metaKey')) {
            $args['meta_key'] = $meta_key;
            $args['meta_value'] = $request->get_param('metaValue');
          }
          return $args;
        },
        10,
        2
      );
    }

(NB the slug name 'rest_mycustomposttype_query' => you'll need to adopt this to your custom post type) and added it in my CPT's class constructor:

add_action('init', array($this, 'mycustomposttype_meta_query_filter'));

Now I can query as such

query.metaKey = 'customMetaKey';
query.metaValue = customMetaValue;

If anybody wants to contribute a more generic approach (multiple key/value meta query parameters) - welcome. This at least is sufficient to solve my current challenge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.