Support

Account

Home Forums Front-end Issues Create a filter by repeater sub field

Solved

Create a filter by repeater sub field

  • 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

    Thank you!

  • Hi @leyzaola

    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);

    I have

     'key'   => 'cobertura_%_pais',
          'compare' => '=',
          'value'   => '5443',

    Just need the value for value => ” because in te DB the value is
    “a:1:{i:0;s:4:”5443″;}”

  • Finally I looks that its working like:

    $args = array(
      'numberposts' => -1,
      'post_type'   => 'noticias',
      'meta_query'  => array(
    
        array(
          'key'   => 'cobertura_%_pais',
          'compare' => '=',
          'value'   => 'a:1:{i:0;s:4:"'.$pais.'";}',
        )
    
      )
    );

    Is that ok?

  • Hi @leyzaola

    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 🙂

  • I doesnt work if I remove this a:1:{i:0;s:4:

    Its working like this:
    'value' => 'a:1:{i:0;s:4:"'.$pais.'";}',

    Thank you very much!

  • Oh I just realize
    the compare should be ‘LIKE’ I had ‘=’

       array(
                                        'key' => 'cobertura_%_pais',
                                        'value' => '"'.$pais.'"',
                                        'compare' => 'LIKE'
                                    )
  • Hi @leyzaola

    Yeah, the compare should be ‘LIKE’. I forgot to change it. I’m sorry about that.

    Did this method solve your issue?

    Thanks 🙂

  • Yes!, how do I close as resolved the thread ?

  • Hi @leyzaola

    The thread doesn’t have “close” status, so you don’t have to do anything as long as you’ve marked it as solved.

    Thanks 🙂

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.