wordpress loop inside foreach statment

Im trying to make a program for a radio show website.
I have a custom field with all the days of week and I just wanna put each post in list of its corresponding day.

So far Iv got the code below and I make 7 lists for all the days but each list has all the same posts. Anyone know how to make this work? Or something else would be better?

Read More
<?php
$days = array(
    'Monday',
    'Tuesday',
    'Wednesday',
    'Thursday',
    'Friday',
    'Saturday',
    'Sunday',
);

$guide = array(
    'post_type' => 'shows',
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'show_day',
            'value' => $dayg,
            'compare' => 'like'
            )
        )
    );
?>

<h3 class="guide">
    <?php the_title(); ?>
</h3>
<?php foreach($days as $dayg) { ?>

    <ul class="day">
        <h3>
            <?php echo $dayg; ?>
        </h3>

        <?php query_posts($guide); while(have_posts()) : the_post(); ?>

        <li><?php the_title(); echo ' | ' , the_field('show_day'); ?></li>

        <?php endwhile; wp_reset_query(); ?>

    </ul>

<?php } ?>

Anyone know

Related posts

Leave a Reply

1 comment

  1. Replace your code with this:–

    <?php
    $days = array(
        'Monday',
        'Tuesday',
        'Wednesday',
        'Thursday',
        'Friday',
        'Saturday',
        'Sunday',
    );
    ?>
    
    <h3 class="guide">
        <?php the_title(); ?>
    </h3>
    <?php foreach($days as $dayg) { ?>
    
    <?php 
    $guide = array(
            'post_type' => 'shows',
            'posts_per_page' => -1,
            'meta_query' => array(
                    array(
                            'key' => 'show_day',
                            'value' => $dayg,
                            'compare' => 'like'
                    )
            )
    );
    ?>
        <ul class="day">
            <h3>
                <?php echo $dayg; ?>
            </h3>
            <?php query_posts($guide); while(have_posts()) : the_post(); ?>
    
            <li><?php the_title(); echo ' | ' , the_field('show_day'); ?></li>
    
            <?php endwhile; wp_reset_query(); ?>
        </ul>
    <?php } ?>
    

    This will work, because you are adding $guide variable outside the loop thats why $dayg is not set…