Support » Fixing WordPress » Group posts by custom field

  • I am trying to group posts based on a meta field. How can i do this?

    Here is my code thus far:

    $args = array(
            'post_type' => 'events',
            'post_status' => 'publish'
        );
        $the_query = new WP_Query($args);
    
        // Build It
        if ($the_query->have_posts()) :
            ?>
            <div id="event-list">
                <?php
                $rows[] = get_post_meta(get_the_ID());
                var_dump($rows);
    
                foreach ($rows as $row) :
                    $newDate = strtotime($row["_edit_lock"][0]);
                    $newMonth = date('F', $newDate);
    
                    while ($the_query->have_posts()) : $the_query->the_post();
                        ?>
    
                        <div class="row">
                            <div class="event-image">
                                <a href="<?php echo get_permalink(get_the_ID()); ?>">
                                    <?php
                                    if (has_post_thumbnail()) {
                                        the_post_thumbnail('thumbnail');
                                    }
                                    ?>
                                </a>
                            </div>
                            <div class="event-content">
                                <h3><a href="<?php echo get_permalink(get_the_ID()); ?>"><?php the_title(); ?></a></h3>
                                <div class="event-date"><?php display_event_date(); ?></div>
                                <div class="event-time"><?php display_event_time(); ?></div>
                                <div class="event-price"><?php display_event_price(); ?></div>
                                <br/>
                                <a href="<?php echo get_permalink(get_the_ID()); ?>" class="event-info">More Information</a>
                            </div>
                            <div class="event-buy">
                                <?php display_event_buy_online_url(); ?>
                            </div>
                        </div>
                    </div>
                    <?php wp_reset_postdata(); ?>
                    <?php
                endwhile;
            endforeach;
        endif;
  • The topic ‘Group posts by custom field’ is closed to new replies.