Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Need help displaying posts based on an ACF field (New Question) WordPress

  • SOLVED

I have the code below that tells WordPress to not show a post if a certain ACF field called 'show_product' has a value of 'no.'

The problem is that I decided to add the show_product field after about 700 products were created (custom posts). I don't want to go edit each one. Is there a way to have this work without editing each product?

I though by making them show if the show_product checkbox wasn't checked would avoid this, but WordPress still wants the database entry for this to work.

$loop = new WP_Query(

array(

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

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

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

)
);


Worst case I could add the show_product ACF field to the quick edit, but that's still going to take forever.

Answers (3)

2015-08-26

Arnav Joy answers:

in this custom post every post will have 'no' ?


Kyler Boudreau comments:

Every post will have a check box called 'show_product' and the only value available will be 'no.'

The problem is that with the code above, it's still not showing products without the checkbox checked because this ACF field was added after the custom post type was published. Does that make sense? If I open a post the new field appears. Then if I immediately save the post (without checking the box) everything is working.


Arnav Joy comments:

if all the posts have same value "no" then why you are using this in query ?


Kyler Boudreau comments:

Because I want to give the site owner the option to hide a product. So the default with be for them all to show, but if they want to edit a product and check the box to hide, they can do that.

2015-08-26

Andrea P answers:

I actually added this bit also in the reply to another of your questions because the first one was closed. ;)

anyway, this should be the completed one, considering the other questions as well.


$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
)
);


Kyler Boudreau comments:

Hey Andrea - stick with me if you can. Your answers have been awesome.

So I just pasted that in, but it doesn't look like it's working quite right. I'm going to make sure I didn't screw something up.


Kyler Boudreau comments:

Andrea, it looks like the part you said can be flaky is being flaky. I'm getting inconsistent results now.


Andrea P comments:

what happens if you take off the not existent part?

I mean take off this:


array (
'key' => 'show_product',
'value' => 'anything',
'compare' => 'NOT EXISTS'

)


Andrea P comments:

ah no!
I made a typo!

this bit is displaying post only id the checkbox is NO, we need the opposite!
change this

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


to this

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


Kyler Boudreau comments:

If I take that off it hides everything. That's the problem... I'm trying to get it to show everything, but it sounds like I'm going to have to do something else to get that to happen.


Andrea P comments:

have you tried keeping the NOT EXISTS part and correcting the typo I made within the no checkbox query?


Andrea P comments:

if it's still not working, we might try to cheat and check if the content of the checkbox array doesn't contain a symbol that we know it would be there if the checkbox field has been stored in the database, whatever is the result

something like


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


Andrea P comments:

suming up, as a first thing, you should try to keep the NOT EXISTS part and correct this bit


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

and put 'NOT LIKE'

then if it doesn't work, you can try the other cheat and this would be the full code then:


$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' => '{',
'compare' => 'NOT LIKE'
)
),
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
)
);





Kyler Boudreau comments:

I think the problem is that since there is no entry in the database at all for this field, no code is going to work. Does that makes sense? Thanks so much for helping Andrea.


Andrea P comments:

so have you tried both the solutions?

what exactly happens with them?
is the code not pulling anything?

if we use NOT EXISTS it should give true if the field does not even exists in the database.
but I think the other code would at the same return true even if the field doesn't exists, because the condition is to check if there isn't a { symbol within that meta field, and if the field doesn't even exists, so that shoudl return true as well, cause surely there can't be any { if the field not even exists..


Kyler Boudreau comments:

Oh really? I tried using the code, but I've been up all night and probably not doing something right. Will try again as that would be awesome if it worked!


Kyler Boudreau comments:

when i put that last code segment in, it shows 7 product and then stops.


Andrea P comments:

does it break or simply it lists only 7 posts and don't include the old ones?

in any case, have you tried the first code I've posted here? I've edited the answer and corrected the NOT LIKE bit in the checkbox conditional meta query


Kyler Boudreau comments:

Figured it out... I had something wrong on my end. Arnav ended up helping me to on this, so I'm going to vote between you both.

For anyone else reading this, Andrea's code works. Just look at the last full segment posted.


Andrea P comments:

ah ok!

just for curiosity, which was the issue?
and have you tried the NOT EXISTS code instead? just because if now that works as well, it is definitely a cleaner way to do it

cheers!


Kyler Boudreau comments:

The issue was that I had a bunch of products missing their identifier (oilblend or oilsingle) and since I was just printing out everything before, I was used to seeing them all, and didn't realize this was missing. Hence the stopping at 7 products in.

What I ended up doing with Arnav's help was changing the checkbox to a radio because ACF serializes the checkbox. Then using some type of query, we wrote a yes or no to every product in the database, thus avoiding the extra checking.

It sounds like either would work, but since my brain was hiccuping I ended up doing the db route first.

2015-08-26

Hariprasad Vijayan answers:

Try thism

$loop = new WP_Query(
array(
'post_type' => 'oils-blends',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'type',
'value' => 'diffuser',
'compare' => 'LIKE'
),
array(
'relation' => 'OR',
array (
'key' => 'show_product',
'value' => 'no',
'compare' => 'NOT LIKE'
),
array(
'key' => 'show_product',
'value' => 'no', // It won't consider, but should not empty
'compare' => 'NOT EXISTS',
),
),
),
'orderby'=>'title',
'order'=>'ASC',
'posts_per_page' => 40
)

);

It consider if show_product is empty. Something like this you are looking for?