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

Need to loop two custom post types based on field WordPress

  • SOLVED

I have two custom post types on my site that share some similar fields. I want to loop in both CPTs based on the value of an ACF field. I also want to pull in the author name.

The custom post types are as follows: admins, users

The ACF field is called badges, and it's a checkbox field. I want to pull people in that have the platinum badge.

My attempted code that isn't working:


<?php

// args
$args = array(
'numberposts' => -1,
'post_type' => 'admins',
'meta_key' => 'badges',
'meta_value' => 'platinum',
'posts_per_page' => 1
);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?php if( $the_query->have_posts() ): ?>

<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<img src="<?php the_field('profile_pic'); ?>" alt="<?php the_title(); ?>">
<?php the_author(); ?>
<?php the_field('state'); ?>

<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>

Answers (2)

2015-04-08

Hariprasad Vijayan answers:

Hi,

Are you two separate loops based on each CPT? Or a single loop that including two CPT?

try this code


<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'admins',
'meta_key' => 'badges',
'meta_value' => array('platinum'),
);
// get results
$the_query = new WP_Query( $args );
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<img src="<?php the_field('profile_pic'); ?>" alt="<?php the_title(); ?>">
<?php the_author(); ?>
<?php the_field('state'); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>


If you are looking to include both CPT in same loop, try following $args in code


$args = array(
'numberposts' => -1,
'post_type' => array( 'users', 'admins' ),
'meta_key' => 'badges',
'meta_value' => array('platinum'),
);


Let me know if you need help.

Regards,
Hariprasad


Kyler Boudreau comments:

Hariprasad,

What you gave me is working....almost. For some reason it's not pulling the meta_key meta_value stuff correctly. I've checked everything...the meta_key is a checkbox. Does that require a different parameter?

Thanks!


Hariprasad Vijayan comments:

Okay,

Try following $args.


$args = array(
'numberposts' => -1,
'post_type' => array( 'users', 'admins' ),
'meta_query' => array(
array(
'key' => 'badges',
'value' => array('platinum'),
'compare' => 'IN',
),
),
);


Kyler Boudreau comments:

Bummer...still nothing. I did try your code pulling another field that wasn't a checkbox, and that seemed to work. So it's something with the checkbox I think.


Kyler Boudreau comments:

I emailed you...signing off for the night. Voting your way as I think you can help me fix this.

2015-04-08

Firoja_Imtosupport answers:

Hi,

Please check if http://www.advancedcustomfields.com/resources/query-posts-custom-fields/ helps you in resolving issue.

Thanks