Hi Im totally lost on trying to create a filter for a custom post type (News) filtered by “Country” or “State” or “City”. I have a repeater (location) and it has those 3 sub fields country, state and city, so What I need to do is filter the news by country. Im doing:
array( 'key' => '_location_%_country', 'value' => 'Dont know the value for the country X', 'compare' => '=' )
I dont know how to call the key value (I saw an exampe) and dont know what is the value for the saved countries
I think knowing those 2 will make it, any clue?
Any help would be apreciated
Have you read this tutorial: https://www.advancedcustomfields.com/resources/query-posts-custom-fields/? Please read it carefully to learn how to query posts based on custom fields.
The value depends on how you get the input from the visitors. If you want, you can use URL parameters and get the value by using the $_GET variable. This page should give you more idea about it: http://php.net/manual/en/reserved.variables.get.php. There’s a tutorial how to filter posts on the archive page, but I believe you can do it on any other pages too. Please take a look at this page to learn more about it: https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/.
I hope this helps 🙂
Great James I have a light. I did it good with a checkbox with fixed values but my sub fields are getting the values from a custom post type
How do I know the values saved for the sub fields in my repeater?, I notice that the key is increasing each time I save a sub field value for country
ie (location_1_country, location_2_country, location_1_country…) And the value saved is something like: i:0;s:4:”5446″; where I check that 5446 is the ID of the country, also it creates another key: _location_1_country, in this case the value is field_576d3054bcc34 but have no idea where it comes from. So my problem is How do I get only the “5446” from the i:0;s:4:”5446″; and where do I look for the _location_1_country value?
Pleae be patient Im not a programmer, but a wordpress enthusiastic!
I think Im getting there I know:
$where = str_replace("meta_key = 'cobertura_%", "meta_key LIKE 'cobertura_%", $where);
'key' => 'cobertura_%_pais', 'compare' => '=', 'value' => '5443',
Just need the value for value => ” because in te DB the value is
The relationship field saves the data as serialized IDs of the post objects. To learn more about serialize, please take a look at this page: http://php.net/manual/en/function.serialize.php.
To search only the ID, I believe you can do it like this:
$args = array( 'numberposts' => -1, 'post_type' => 'noticias', 'meta_query' => array( array( 'key' => 'cobertura_%_pais', 'compare' => '=', 'value' => '"' . $pais . '"', ) ) );
To learn more about querying relationship field, please take a look at this page: https://www.advancedcustomfields.com/resources/querying-relationship-fields/.
Also, “_location_1_country” is a reference key used by ACF to save and load the right value, so you don’t need to worry about it.
I hope this helps 🙂
The topic ‘Create a filter by repeater sub field’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!