Support

Account

Home Forums General Issues Nested WP_Query with custom fields

Solved

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(). ?>
  • Hi @jessiewillms

    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 🙂

  • Thank you SO much! That solved it!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Nested WP_Query with custom fields’ is closed to new replies.