Query with meta fields (ACF)
-
Hey, I want to filter the query to posts with special values on meta fields (ACF).
How can I do this?
-
Hey @wp_user1
We don’t have a UI for meta queries, but if you’re comfortable with WordPress hooks, and a bit of PHP, then you could use our filter to modify the query:
https://customlayouts.com/documentation/action-filter-reference/
It’s the third one –
custom-layouts/layout/query_args
I hope that helps!
Best
Sounds good.
But what if I want different views on the same page?
Can I get the filter ID in the layout_query_args function? So that I can change the query for different layouts?
Yup, its undocumented (because we plan to change some of this implementation in the near future) but you can get the layout ID as the second parameter – here is our code:
$query_args = apply_filters( 'custom-layouts/layout/query_args', $query_args, $this->id );
Obviously this wouldn’t work if you were building a layout directly in the gutenberg editor, because there is no associated ID – if that is what you’re doing (?) then I’ll consider adding some extra info to this filter to make it easier.
Best
So I tried it.
The first method works great:
function layout_query_args( $query_args ) { $query_args['meta_key'] = 'keyname'; $query_args['meta_value'] = 'value'; $query_args['meta_compare'] = '='; return $query_args; } add_filter( 'custom-layouts/layout/query_args', 'layout_query_args', 10 );
But this applies to ALL custom layouts.
I need different custom layouts on the same page.
So how can I get the information about the Custom Layout ID in the layout_query_args function?
For example:
Test page.. Layout 1: [custom-layout id='860'] test test Layout 2: [custom-layout id='861']
Now ID=860 should show values with meta_value=’value 1′ and ID=861 should show values with meta_value=’value 2′
Hey @wp_user1
So you would just need to modify your filter like this:
function layout_query_args( $query_args, $id ) { $query_args['meta_key'] = 'keyname'; $query_args['meta_value'] = 'value'; $query_args['meta_compare'] = '='; return $query_args; } add_filter( 'custom-layouts/layout/query_args', 'layout_query_args', 10, 2 );
Notice, I added the
2
toadd_filter
and then added$id
to the functionlayout_query_args
Please watch out for our next major version (and I’ll try to remember to update this ticket) as some of these apis might change – we won’t remove any functionality though, probably just rename the filter and perhaps change the order of the arguemnts.
Best
Okay thank you – I changed it to
function layout_query_args( $query_args, $id )
and
add_filter( 'custom-layouts/layout/query_args', 'layout_query_args', 10, true );
This seems to work fine. Even I am not so sure, if this is correct, to have the “true” as value? I tested and it did not matter, which value I chose.
The
true
should be a2
… I’m suprised that works!So it should be:
add_filter( 'custom-layouts/layout/query_args', 'layout_query_args', 10, 2 );
Best
I also want to filter by a custom field. I have a custom post type “events” and want to sort them by a datetime customfield. I have a working filter, but similar to “wp_user1” all other loops on the side are not working.
$query_args['post_type'] = array('events'); $query_args['meta_key'] = 'events_time'; $query_args['orderby'] = 'meta_value'; $query_args['order'] = 'asc';
I am not able to apply that filter only to loops that query the custom post type “events”. I tried to add it with on the condition
get_post_type() === 'events'
and some other ways. Is there another undocumented API I could use to accomplish this. I created all loops in Gutenberg, to replace all of them with an ID would be a lot of work.Hi @ja4st3r
I need to have a think about the apis for all this (which is why some things are undocumented), but I think you could have some success with the following:
1. Check what is already in
$query_args
and apply your changes conditionally.If the gutenberg layout you want to target, is the only one displaying
events
, then you can check it the$query_args['post_type'] === 'events
(it might be an arrayarray( 'events' )
) and then you could add your ordering parameters.2. Alternatively, you could use the
before_render
hook, get the extra info about the layout there (this has an undocumented paramater which gives you all the layout settings).. and then add yourquery_args
filter there.Then make sure to remove the query_args hook in the
after_render
hook for the layout (so you don’t affect all layouts queries).Let me know if that all makes sense?
For reference, all our hooks are here (some with undocumented parameters): https://customlayouts.com/documentation/action-filter-reference/
Best
– RossHi Ross,
thank you for your suggestions. I tried the first one and it worked liked a charm. I didnt think about using
$query_args['post_type'] === 'events
as a condition before.Thank your very much for your help!
- The topic ‘Query with meta fields (ACF)’ is closed to new replies.