0

I'm working on a site with photo contests. Each contest has a lot of additional info, which I store in custom fields (thanks to Advanced Custom Fields). Now I want to search through contests filtering them based on some custom fields values.

My function.php file looks like this:

// Custom Post Search
add_action('pre_get_posts', 'my_pre_get_posts');

function my_pre_get_posts( $query )
{
    // validate
    if( is_admin() )
    {
        return;
    }

    if( !$query->is_main_query() )
    {
        return;
    }

    if( !empty($_GET['fee']) )
    {
        $contestFee = explode(',', $_GET['fee']);

        $contestFeeArray = array(
            'key'       => 'fee',
            'value'     => $contestFee,
            'compare'   => 'IN',
        );
    }
    if( !empty($_GET['theme']) )
    {
        $contestTheme = explode(',', $_GET['theme']);

        $contestThemeArray = array(
            'key'       => 'theme',
            'value'     => $contestTheme,
            'compare'   => 'IN',
        );
    }

    $meta_query[] = array(
        'relation' => 'OR',
        $contestThemeArray,
        $contestFeeArray,
    );

    $query->set('meta_query', $meta_query);

    return;

}
?>

Now I'm quering my search like this website.com/contests/?fee=free&theme=landscape and here's the problem. If I query only fee value everything works perfectly. But when I want to query theme value nothing is displayed.

I'm pasting also arrays displayed through get_field_object (which is an Advanced Custom Fields' function):

fee custom field

Array ( [ID] => 136 [key] => field_553bee3a1f8ff [label] => Is contest paid or free? [name] => fee [prefix] => [type] => radio [value] => free [menu_order] => 7 [instructions] => [required] => 1 [id] => [class] => [conditional_logic] => 0 [parent] => 8 [wrapper] => Array ( [width] => [class] => [id] => ) [_name] => fee [_input] => [_valid] => 1 [choices] => Array ( [free] => Free [paid] => Paid ) [other_choice] => 0 [save_other_choice] => 0 [default_value] => free [layout] => vertical )

theme custom field

Array ( [ID] => 23 [key] => field_5523fed672b4b [label] => Theme [name] => theme [prefix] => [type] => select [value] => Array ( [0] => nude [1] => people [2] => fine-art ) [menu_order] => 12 [instructions] => [required] => 1 [id] => [class] => [conditional_logic] => 0 [parent] => 8 [wrapper] => Array ( [width] => [class] => [id] => ) [_name] => theme [_input] => [_valid] => 1 [choices] => Array ( [aerial] => Aerial [fine-art] => Fine Art [landscape] => Landscape [nature] => Nature [nude] => Nude [people] => People [wildlife] => Wildlife ) [default_value] => Array ( [] => ) [allow_null] => 0 [multiple] => 1 [ui] => 1 [ajax] => 1 [placeholder] => [disabled] => 0 [readonly] => 0 )

So to wrap it up - I want to query custom posts (contests) search through website.com/contests/?fee=free&theme=landscape and in this case display contest that are free AND have landscape theme.

I can't find out what's wrong with my code. Do you have any ideas on making this work?

Thanks!

closed as off-topic by Pieter Goosen, Mayeenul Islam, Howdy_McGee, TheDeadMedic, Robert hue May 7 '15 at 6:27

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    theme is already a WordPress query var, use something more unique. – Milo Apr 26 '15 at 15:00
  • I've changed it to contest_theme but it still doesn't work. – bypatryk Apr 26 '15 at 20:18
  • @Milo you can check out the site here - (patryk.me/guru/contests) – bypatryk Apr 26 '15 at 20:22
  • @Milo Ok, I've finally got it, but still have a little problem. ACF postmeta which can have multiple values are stored in MySQL like this: a:1:{i:0;s:9:"Landscape";} or a:5:{i:0;s:6:"Aerial";i:1;s:6:"Nature";i:2;s:4:"Nude";i:3;s:6:"People";i:4;s:8:"Wildlife";}. The problem that remains is how to query contest_theme with multiple values (like "Nature", "Nude", etc.) – bypatryk Apr 26 '15 at 22:33
1

Ok, so after deeper research I've found out that values of some custom fields created by ACF are stored in MySQL database with some additional information like a:5:{i:0;s:6:"Aerial";i:1;s:6:"Nature";i:2;s:4:"Nude";i:3;s:6:"People";i:4;s:8:‌​"Wildlife";}. This happens when a custom field can store multiple values (like chechboxes).

So I've edited part of my query from this: if( !empty($_GET['theme']) ) { $contestTheme = explode(',', $_GET['theme']);

    $contestThemeArray = array(
        'key'       => 'theme',
        'value'     => $contestTheme,
        'compare'   => 'IN',
    );
}

To this:

if( !empty($_GET['contest_theme']) )
{
    $contestTheme = explode(',', $_GET['contest_theme']);
    $contestThemeArray = array('relation' => 'OR');
    foreach ($contestTheme as $theme) {
        $contestThemeArray[] = array(
            'key'       => 'contest_theme',
            'value'     => $theme,
            'compare'   => 'LIKE',
        );
    }

}

Not the answer you're looking for? Browse other questions tagged or ask your own question.