Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using Advanced Custom Fields and have been following along with this tutorial on Creating a WP archive with Custom Field Filter. In their example, they're using Radio Button Field; I would like to use the Checkbox Field for a custom post type called 'book', as some of my books have more than one 'book_type' associated with them.

Field name: book_type

Field type: Checkbox Field

Choices: men, women, children, accessories

My functions.php page looks like this:

add_action('pre_get_posts', 'my_pre_get_posts');

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

$book_format = $query->get('meta_query');
    if( !empty($_GET['book_type']) ) {
        $book_format[] = array(
            'key'       => 'type',
            'value'     => $_GET['book_type'],
            'compare'   => 'LIKE',
        );
    }

$query->set('meta_query', $book_format);
return;
}

Now, when I manually add my 'book_type' to my url like this:

dev.com/books?=men

it successfully shows all books related to men. But when I add the '|' seperator, as you do with an array, I get a blank page.

dev.com/books?=men|women|accessories

Am I doing something wrong here?

Any help is appreciated. Thanks in advance!

share|improve this question
    
the link you give use checkbox in the code snippet –  nackle Jan 8 at 22:13
    
No, that's a checkbox for the page template. To check/uncheck multiple choices on the front-end. I'm trying to use a Checkbox Field when they're using a Radio Field to retrieve the values. –  realph Jan 8 at 22:22
    
I've edited my original question above, to help explain the issue a little further. –  realph Jan 9 at 15:53

1 Answer 1

up vote 1 down vote accepted

If you're receiving an array, your meta_query is going to change. LIKE will no longer be valid, you'd need to do an IN comparison at that point.

Also I'm not entirely convinced you're correctly passing a $_GET array

share|improve this answer
    
I've tried an IN comparison, but no results are shown. I just get a blank page. –  realph Jan 9 at 16:22
    
If you get a blank page, you should check your logs for the error report... You do have debugging enabled, right? Also, updated answer. –  GhostToast Jan 9 at 16:24
    
I have debugging enabled. Not getting an error on the "?books=men" page, but am getting an "Undefined index" error in relation to the $_GET param I'm using on my archive page template. –  realph Jan 9 at 16:31
    
I've been trying to work out how to print that $_GET array, so I can check it. Can I not do it in Chrome's Console? Sorry for these silly questions, I'm still trying to wrap my head around PHP and this is my first time in the trenches. –  realph Jan 9 at 16:36
    
Chrome's console won't help with PHP, only javascript and CSS. Go read your logs, as you don't always get output –  GhostToast Jan 9 at 16:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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