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

Need to loop CPT based on custom field WordPress

  • SOLVED

I have a custom post type with an ACF (Advanced Custom Fields) checkbox. I want to loop posts based on the checkbox. Here's what I have that isn't working:

The post type is 'oil-products' and the ACF checkbox is called 'type'.



<?php

$loop = new WP_Query( array( 'post_type' => 'oil-products','meta_key' => 'type','in_array' => 'Beauty','orderby'=>'title','order'=>'ASC','posts_per_page' => 40 ) );
while ( $loop->have_posts() ) : $loop->the_post();

echo '<div class="oil-products-container">';
echo '<div class="oil-products-image"><a href="';
the_permalink();
echo '"><img src="';
the_field('image');
echo '" alt="';
the_title();
echo '"></a></div>';
echo '<div class="oil-products-title"><a href="';
the_permalink();
echo '" class="black-link">';
the_field('short_name');
echo '</a></div></div>';
endwhile;
?>

<?php endwhile; ?>

Answers (3)

2015-06-18

Romel Apuya answers:

use

$loop = new WP_Query(
array(
'post_type' => 'oil-products',
'meta_key' => 'type',
'in_array' => 'Beauty',
'orderby'=>'meta_value_num',
'order'=>'ASC',
'posts_per_page' => 40
)
);


[[LINK href="http://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/"]]reference[[/LINK]]


Kyler Boudreau comments:

Romel,

This doesn't work. Just shows them all.

2015-06-18

timDesain Nanang answers:

<?php
$loop = new WP_Query( array(
'post_type' => 'oil-products',
'meta_key' => 'type',
'meta_value' => '"Beauty"', // '"Beauty"' or 'Beauty'
'meta_compare' => 'LIKE',
'orderby'=>'title',
'order'=>'ASC',
'posts_per_page' => 40
) );
while ( $loop->have_posts() ) : $loop->the_post();
//do stuff
endwhile;
?>


Kyler Boudreau comments:

timDesain,

My page white screens with this code.


timDesain Nanang comments:

your code has <strong>endwhile;</strong> twice.
try to remove the last one
and set <strong>define('WP_DEBUG', true);</strong> to show the error


Kyler Boudreau comments:

Here's what I have:


<?php

$loop = new WP_Query( array(

'post_type' => 'oil-products',

'meta_key' => 'type',

'meta_value' => '"Beauty"', // '"Beauty"' or 'Beauty'

'meta_compare' => 'LIKE',

'orderby'=>'title',

'order'=>'ASC',

'posts_per_page' => 40

) );

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

//do stuff

endwhile;

?>


Kyler Boudreau comments:

This worked, which was a combination of both answers - thanks!


<?php

$loop = new WP_Query(

array(

'post_type' => 'oil-products',
'meta_key' => 'type',
'meta_value' => '"Diffuser"', // '"Beauty"' or 'Beauty'
'meta_compare' => 'LIKE',
'orderby'=>'title',
'order'=>'ASC',
'posts_per_page' => 40

)

);


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

echo '<div class="oil-products-container">';
echo '<div class="oil-products-image"><a href="';
the_permalink();
echo '"><img src="';
the_field('image');
echo '" alt="';
the_title();
echo '"></a></div>';
echo '<div class="oil-products-title"><a href="';
the_permalink();
echo '" class="black-link">';
the_field('short_name');
echo '</a></div></div>';
endwhile;
?>

<?php endwhile; ?>


timDesain Nanang comments:

I see, the code not give an output.
you should add your code inside the loop to show the output
<?php
$loop = new WP_Query( array(
'post_type' => 'post',
'meta_key' => 'type',
'meta_value' => '"Black"', // '"Beauty"' or 'Beauty'
'meta_compare' => 'LIKE',
'orderby'=>'title',
'order'=>'ASC',
'posts_per_page' => 40
) );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="oil-products-container">';
echo '<div class="oil-products-image"><a href="';
the_permalink();
echo '"><img src="';
the_field('image');
echo '" alt="';
the_title();
echo '"></a></div>';
echo '<div class="oil-products-title"><a href="';
the_permalink();
echo '" class="black-link">';
the_field('short_name');
echo '</a></div></div>';
endwhile;
?>

2015-06-18

Reigel Gallarde answers:

try meta_query like this....
$loop = new WP_Query(
array(
'post_type' => 'oil-products',
'orderby'=>'title',
'order'=>'ASC',
'posts_per_page' => 40,
'meta_query' => array(
array(
'key' => 'type',
'value' => array('Beauty'),
'compare' => 'IN',
)
)
)

);