WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Filter by Custom Field With Multiple Fields (2 posts)

  1. kettlewell
    Member
    Posted 2 years ago #

    I'm trying to filter (in the admin panel) by a custom field that is part of a checkbox group.

    So the checkbox group could contain "one" or "one,two" (for example)

    I found some code that allows me to filter an exact match (like "one" ), but I'd like to be able to have a value box that takes "two" as a parameter, and will find any post that is part of that checkbox group (like "one,two").

    I think I'm close, but I'm off on my syntax (I think).

    This is inside of a filter function that takes a $query as a parameter.

    The essence is that I'd like the query to resemble:

    select * where key=field_name and value LIKE field_value

    Based on an article that Otto wrote, I have this...

    $query = wp_parse_args($query_string);
    
    $query['meta_query'] = array(
                             array(
    	                 'key' => $_GET['FIELD_NAME'],
    	                 'value' => $_GET['FIELD_VALUE'],
    	                 'compare' => 'LIKE',
                                  ),
                                );

    Thoughts, ideas ?

    Thanks

    Matt

  2. vtxyzzy
    Member
    Posted 2 years ago #

    If what you want is to find posts that have meta_values of EITHER 'one' OR 'two' OR both, you will need code like this:

    $key = $_GET['FIELD_NAME'];
    $values = $_GET['FIELD_VALUES'];
    $parts = explode(',',$values);
    $meta_query = array();
    if ($parts[0]) {
       $meta_query[] = array(
          'key' => $key,
          'value' => $parts[0],
          'compare' => 'LIKE'
       );
    }
    if ($parts[1]) {
       $meta_query[] = array(
          'key' => $key,
          'value' => $parts[1],
          'compare' => 'LIKE'
       );
       $meta_query['relation'] = 'OR';
    }
    $query['meta_query'] = $meta_query;

Topic Closed

This topic has been closed to new replies.

About this Topic