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

Need to show custom post type based on check box WordPress

  • SOLVED

I have a custom post type loop that I want to add an argument telling it to only show posts based on an Advanced Custom Field (ACF) checkbox.

The name of the checkbox is 'Show Product (show_product)' and the value is 'No.' So if checked, the product would not show.

Here's my working custom post type loop:



<?php
$loop = new WP_Query(
array(

'post_type' => 'oils-blends',
'meta_key' => 'type',
'meta_value' => 'oilblend',
'meta_compare' => 'LIKE',
'orderby'=>'title',
'order'=>'ASC',
'posts_per_page' => 40
)
);

while ( $loop->have_posts() ) : $loop->the_post();

echo '<div class="everyday-oil-container">';
echo '<div class="everyday-oil-image"><a href="';
the_permalink();
echo '"><img src="';
if( get_field('oil_image') )
the_field('oil_image');
else
the_field('main_image_link');
echo '" alt="';
the_title();
echo '"></a></div>';
echo '<div class="everyday-oil-title" itemscope itemtype="http://schema.org/HealthAndBeautyBusiness"><a href="';
the_permalink();
echo '" class="black-link"><span itemprop="makesOffer">';
the_field('short_name');
echo '</span></a></div></div>';
endwhile;
?>

<?php endwhile; ?>



So to summarize, only custom posts with the No checkbox checked should show.

Answers (2)

2015-08-26

Andrea P answers:

Hi There!

the principle is the very same you have used to filter by the checkbox "type", you just have to add a meta_query instead of a single meta field, so that you can filter by multiple meta fields:


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




note: I've added double quotes to avoid false positves (very difficult to happen though..). the format of an acf checkbox field in the database looks something like this:
a:1:{i:0;s:17:"oilblend":18:"secondoilblend";}

so by instance, if you search for
oilblend
it will give positive for both options. while searching for
"oilblend"
will give positive only if the first is selected

cheers!


Andrea P comments:

I've edited my answer correcting a couple of typos. it should work fine now ;)


Kyler Boudreau comments:

Andrea, you're a genius and a God send.

This worked perfectly.

THANKS.


Kyler Boudreau comments:

Andrea,

One other question if you don't mind -- i have a bunch of pre-existing posts. I just added this checkbox field, but don't want to have to go edit all of them. Even though I'm only not showing those with the 'No' checkbox, the ones that don't have a database entry for this new field either way are still not showing. Does that make sense? Is there a way around that? I can pay extra if needed.

2015-08-26

Hariprasad Vijayan answers:

Hello,

Try this


<?php

$loop = new WP_Query(

array(



'post_type' => 'oils-blends',

'orderby'=>'title',

'order'=>'ASC',

'posts_per_page' => 40,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'show_product',
'value' => 'Yes',
'compare' => '=',
'type' => 'CHAR',
),
array(
'meta_key' => 'type',
'meta_value' => 'oilblend',
'meta_compare' => 'LIKE',
'type' => 'CHAR',
),
),

)

);



while ( $loop->have_posts() ) : $loop->the_post();



echo '<div class="everyday-oil-container">';

echo '<div class="everyday-oil-image"><a href="';

the_permalink();

echo '"><img src="';

if( get_field('oil_image') )

the_field('oil_image');

else

the_field('main_image_link');

echo '" alt="';

the_title();

echo '"></a></div>';

echo '<div class="everyday-oil-title" itemscope itemtype="http://schema.org/HealthAndBeautyBusiness"><a href="';

the_permalink();

echo '" class="black-link"><span itemprop="makesOffer">';

the_field('short_name');

echo '</span></a></div></div>';

endwhile;

?>



<?php endwhile; ?>


Kyler Boudreau comments:

I'm sorry, I meant I need it to show if the checkbox is not checked.


Kyler Boudreau comments:

Actually, the yes method works fine. Let me check it real fast. Thanks!


Hariprasad Vijayan comments:

Have you tried placing "No" to meta query?


<?php

$loop = new WP_Query(

array(



'post_type' => 'oils-blends',

'orderby'=>'title',

'order'=>'ASC',

'posts_per_page' => 40,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'show_product',
'value' => 'No',
'compare' => '=',
'type' => 'CHAR',
),
array(
'meta_key' => 'type',
'meta_value' => 'oilblend',
'meta_compare' => 'LIKE',
'type' => 'CHAR',
),
),

)

);



while ( $loop->have_posts() ) : $loop->the_post();



echo '<div class="everyday-oil-container">';

echo '<div class="everyday-oil-image"><a href="';

the_permalink();

echo '"><img src="';

if( get_field('oil_image') )

the_field('oil_image');

else

the_field('main_image_link');

echo '" alt="';

the_title();

echo '"></a></div>';

echo '<div class="everyday-oil-title" itemscope itemtype="http://schema.org/HealthAndBeautyBusiness"><a href="';

the_permalink();

echo '" class="black-link"><span itemprop="makesOffer">';

the_field('short_name');

echo '</span></a></div></div>';

endwhile;

?>



<?php endwhile; ?>


Kyler Boudreau comments:

I tried the code, but it doesn't work with the Yes.

Ideally, what I'm after is that it shows the posts unless the "No" checkbox is checked.

Is that possible?