Home › Forums › General Issues › Nested WP_Query with custom fields
Hi,
I am trying to use a nest WP Query to get two custom post types (unit and lesson) and sort them by the custom fields (unit_month and lesson_month). Trying to get the custom post for the unit and the posts for the lesson, both with the value of February in their custom fields (unit_month or lesson_month).
This is what I have currently. Currently it will pull in the months just fine, but under the months (the h5), it will pull *all* the lessons, not just the lessons for with that month.
I want to output to be:
Month (February)
Feb. Lesson #1
Feb. Lesson #2
Feb. Lesson #3 …
Month #2 (January)
Jan. Lesson #1
Jan. Lesson #2
Jan. Lesson #3 …
Any help appreciated.
<?php
$args = array(
'numberposts' => -1,
'post_type' => 'unit',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'unit_month',
'value' => 'January',
'compare' => 'LIKE'
),
array(
'key' => 'unit_month',
'type' => 'February',
'compare' => 'LIKE'
)
)
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php $pub_id = get_the_ID(); ?>
<a href="<?php the_permalink(); ?>">
<h5><?php the_title(); ?></h5>
</a>
<?php $issue = new WP_Query( array(
'numberposts' => -1,
'post_type' => 'lesson',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'lesson_month',
'value' => 'January',
'compare' => 'LIKE'
),
array(
'key' => 'lesson_month',
'type' => 'February',
'compare' => 'LIKE'
)
)
) ); ?>
<?php
if ( $issue->have_posts() ) ?>
<?php while ( $issue->have_posts() ) : $issue->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; ?>
<?php $issue->reset_postdata(); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
I believe you can achieve it by providing the ‘lesson_month’ value dynamically. Maybe something like this:
$unit_month = get_field('unit_month', $pub_id);
$issue = new WP_Query( array(
'numberposts' => -1,
'post_type' => 'lesson',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'lesson_month',
'value' => $unit_month,
'compare' => 'LIKE'
)
)
) );
I hope this helps 🙂
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 7, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.