Support » Fixing WordPress » Using multiple loops, only the first grabs posts

  • Resolved Mr_Specific

    (@mr_specific)



    I’m trying to do a complicated page – I am querying a custom post type, and I need to run multiple loops to pull events from specific dates ( dates are set up as a custom taxonomy) and specific locations (another taxonomy). It works great for the first time through (the first date), but for all the rest of the dates, it doesn’t find any posts. I know that the problem is with the loops after the first one, because if I change the order, the first loop always works, and the rest do not. I’m really pulling my hair out on this one, so I’d very much appreciate any help!
    I’ve tried using wp_reset_postdata, rewind_posts() and wp_reset_query() after each query, and they don’t seem to have any effect.
    Here is the pertinent code:

    <ul id="event_list">
    <?php
        $current_location = get_field( 'event_location_id' );
        $date_ids = dc_event_days();
        foreach( $date_ids as $value ){
    
            $value = explode( ":", $value );
            if( empty($value[0]) || empty($value[1]) ){ continue; }
            $id = trim(str_replace(array('.', ' ', "\n", "\t", "\r"), '', $value[0]));
            $name = $value[1];
            $event_list = "<li class=\"event_day\"><h2 class=\"event_day_title\">$name</h2>";
    
            if( !empty($current_location) ){
                $tax_query =
                    array(
                        'relation' => 'AND',
                        array(
                            'taxonomy' => 'event_day',
                            'field' => 'id',
                            'terms' => $id
                        ),
                        array(
                            'taxonomy' => 'event_location',
                            'field' => 'id',
                            'terms' => $current_location,
                        )
                    );
            }else{
                 $tax_query = array(
                        'relation' => 'AND',
                        array(
                            'taxonomy' => 'event_day',
                            'field' => 'id',
                            'terms' => $id
                        )
                    );
            }
            $args = array('post_type' => 'event',
                'tax_query' => $tax_query,
            );
            //var_dump($tax_query);
    
            unset($event_loop);
            $event_loop = new WP_Query($args);
            if( $event_loop->have_posts() ) {
                while($event_loop->have_posts()) : $event_loop->the_post();
                    $event_list .= '<a href="'.get_permalink().'">'.get_the_title().'</a>';
                endwhile;
            }
            wp_reset_postdata();
    
            $event_list .= "</li>";
            echo $event_list;
            var_dump($args);
        }
    ?>
    </ul>

Viewing 2 replies - 1 through 2 (of 2 total)
  • As a guess, $date_ids is a different format than you expect. Try dumping it out to verify the contents.

    Thank you, vtxyzzy – that did end up being the solution! That was a list of ID’s pulled from a nav menu, and wordpress was putting some content in there I wasn’t expecting.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Using multiple loops, only the first grabs posts’ is closed to new replies.