Ask your WordPress questions! Pay money and get answers fast! (more info)

Need to loop custom post types - multiple checkbox array

I have a custom post type with an Advanced Custom Fields (ACF) checkbox specifying a type.

Currently, I can show posts based on a type such as 'oilsingle' or 'oilblend' which are part of the ACF checkbox.

I want to loop posts that are either oilsingle or oilblend, but I'm not sure how to adjust the arg stuff in my loop. I can only get it to do one or the other right now.

The code below works for a single:



$loop = new WP_Query(

array(

'post_type' => 'oils-blends',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'type',
'value' => '"oilsingle"',
'compare' => 'LIKE'
),

array (
'key' => 'show_product',
'value' => '"no"',
'compare' => 'NOT LIKE'
)
),

'orderby'=>'title',
'order'=>'ASC',
'posts_per_page' => 40
)
);

Answers (3)

2015-08-26

S├ębastien | French WordpressDesigner answers:

in my code the name of your post-type is "oilsingle"
but change this word if it's another name


$loop = new WP_Query(



array(




'post_type' => array( 'oils-blends', 'oilsingle' ),

'meta_query' => array(

'relation' => 'AND',

array(

'key' => 'type',

'value' => '"oilsingle"',

'compare' => 'LIKE'

),



array (

'key' => 'show_product',

'value' => '"no"',

'compare' => 'NOT LIKE'

)

),



'orderby'=>'title',

'order'=>'ASC',

'posts_per_page' => 40

)

);


Kyler Boudreau comments:

Sebastian,

I apologize -- I didn't describe this well.

What I need is is the value to be more than one. This line:

'value' => '"oilsingle"',

2015-08-26

Andrea P answers:

$loop = new WP_Query(
array(
'post_type' => 'oils-blends',
'meta_query' => array(
'relation' => 'AND',
array (
'relation' => 'OR',
array (
'key' => 'show_product',
'value' => '"no"',
'compare' => 'NOT LIKE'
),
array (
'key' => 'show_product',
'value' => 'anything',
'compare' => 'NOT EXISTS'
)
),
array(
'relation' => 'OR',
array(
'key' => 'type',
'value' => '"oilblend"',
'compare' => 'LIKE'
),
array(
'key' => 'type',
'value' => '"oilsingle"',
'compare' => 'LIKE'
)
)
),
'orderby'=>'title',
'order'=>'ASC',
'posts_per_page' => 40
)
);


Andrea P comments:

edited my answer.
there was an error in how I nested the arrays, and then I've also added the option that it will display the post if the checkbox is "no" or NOT EXISTENT.

in any case, I've read that NOT EXISTENT may give some problems, so it's not sure that it will work..


Andrea P comments:

I think that for this part of the problem, my code did the trick. even though I added here the part regarding the "no" checkbox too, which then we've actually discussed and sorted in another post

2015-08-27

Arnav Joy answers:

is this done?