• Resolved wp_user1

    (@wp_user1)


    Hey, I want to filter the query to posts with special values on meta fields (ACF).

    How can I do this?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Code Amp

    (@codeamp)

    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

    Thread Starter wp_user1

    (@wp_user1)

    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?

    Plugin Author Code Amp

    (@codeamp)

    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

    Thread Starter wp_user1

    (@wp_user1)

    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′

    Plugin Author Code Amp

    (@codeamp)

    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 to add_filter and then added $id to the function layout_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

    Thread Starter wp_user1

    (@wp_user1)

    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.

    Plugin Author Code Amp

    (@codeamp)

    The true should be a 2… 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.

    Plugin Author Code Amp

    (@codeamp)

    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 array array( '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 your query_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
    – Ross

    • This reply was modified 3 years, 5 months ago by Code Amp.
    • This reply was modified 3 years, 5 months ago by Code Amp.

    Hi 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!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Query with meta fields (ACF)’ is closed to new replies.