0

I wanted to learn more about querying post types and their custom fields in WordPress. I've got two questions...

I've followed this tutorial on Advanced Custom Fields and am trying to understand what is happening here. This is placed in my theme's function file.

add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts( $query ) {
    if( is_admin() ) {
        return;
    }

    if( isset($_GET['type']) ) {
        $type = explode(',', $_GET['type']);

        $meta_query[] = array(
            'key'       => 'type',
            'value'     => $type,
            'compare'   => 'IN',
        );
    }

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

    return; // always return
}

Now I understand this is somehow allowing the URL to alter the query based on its custom field (in this case type) and values that are assigned to this post. But how exactly is it working?

My second question is, when I pass this query in my URL like so:

http://example.com/cars?type=coupe,hatchback

it returns all Coupe and Hatchback cars – which is great! This seems to work because my custom field (type) only accepts one value. A car has to be either a Coupe or a Hatchback – it can't be both.

On the other hand, some of my custom fields (take for, for example) can hold more than one value. My for custom field can actually take up to five values ("him", "her", "families" etc). For some reason, that code above doesn't play nice when assigned to custom field (like for) that can have multiple values. This doesn't work, at all:

http://example.com/cars?for=him,her

Any ideas why that is and what I have to do to get it to work?

Thanks in advance.

EDIT:

Matching multiple values by running the LIKE query:

add_action('pre_get_posts', 'my_pre_get_posts');

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

if( isset($_GET['type']) ) {                        
    $meta_query[] = array(
        'key'       => 'type',
        'value' => '"' . $_GET['type'] . '"',
        'compare'   => 'LIKE',
        );
}
$query->set('meta_query', $meta_query);

return;
}
  • There is code involved that you have not posted. In fact, what you posted wouldn't do anything at all except take up a tiny bit of processing power. To work, that code needs to be hooked into pre_get_posts, or used as part of a new WP_Query or passed to query_posts. Don't ever do the latter. Most of what you need is likely on the the Codex page for WP_Query and in the PHP docs. – s_ha_dum Jan 11 '14 at 2:31
  • Sorry, my bad, I've updated it with the full code block. – realph Jan 11 '14 at 2:34
  • If this is an ACF checkbox field (assuming this is related to your other questions), those values are saved as serialized data in a single field, so you can't make that work with a meta query. in hindsight, that was probably your issue with those other questions, sorry it didn't dawn on me sooner. – Milo Jan 11 '14 at 2:47
  • @Milo That makes sense. Though, I did see a [post on the ACF forum][1] from the plugin author that mentioned sending multiple values through like this: website.com?foo=bar|bar2|bar3 and exploding via the '|'. Would that be possible? [1]: support.advancedcustomfields.com/forums/topic/… – realph Jan 11 '14 at 3:49
  • I don't see how that's any different from passing them comma-separated. anyway, the issue is that the data is saved in a format that only php "understands", so MySQL can't query that data. – Milo Jan 11 '14 at 4:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.