2

I want to combine the 2 following queries so that ongoing promotions (query 1) shows before the rest of the promotions (query 2) but all together no more than 5 results show. Thanks.

Query #1 Ongoing Promotions

<?php
    global $post;
    $args = array(
        'post_type' => 'promotions',
        'numberposts' => 5,
        'meta_key' => 'sp_ongoingPromotion',
        'meta_value' => 1
    );
    $supplierCommunications = get_posts( $args );
    foreach( $supplierCommunications as $post ) :   setup_postdata($post);
?>
<li>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br/>
    <span>
        <?php
            echo "Ongoing Promotion";
        ?>
    </span>
</li>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>

Query #2 Current Promotions

            <?php
                global $post;
                $args = array(
                    'post_type' => 'promotions',
                    'numberposts' => 5,
                    'meta_key' => 'sp_endDate',
                    'meta_value' => date("Y/m/d"),
                    'meta_compare' => '>=',
                    'orderby' => 'meta_value',
                    'order' => 'ASC'
                );
                $supplierCommunications = get_posts( $args );
                foreach( $supplierCommunications as $post ) :   setup_postdata($post);
            ?>
            <li>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <br/>
                <span>
                    <?php
                        $meta = get_post_meta(get_the_ID(), 'sp_endDate', true);
                        if('' != $meta) {
                            $formattedDate = new DateTime($meta);
                            echo "Expires: ".$formattedDate->format('F d, Y');
                        }
                    ?>
                </span>
            </li>
            <?php endforeach; ?>
            <?php wp_reset_postdata(); ?>
2

I wouldn't combine the queries, primarily since they're keying on different meta keys and values. The WP Query API is very powerful, but you're trying to do some very advanced searching and sorting here.

One option would be to write a raw SQL query by hand and pass it into $wpdb->get_results(), but that won't necessarily be future safe (i.e. if future versions change the DB schema).

Instead, I recommend you do both queries separately and merge the two arrays.

Some (very rough) psuedocode:

// Get your promotions using the arguments you outlined above.
$ongoing_promotions = get_posts( $ongoing_args );
$current_promotions = get_posts( $current_args );

// Merge the two arrays
$all_promotions = array_merge( $ongoing_promotions, $current_promotions );

// Get just the IDs of your promotions
$promotion_ids = wp_list_pluck( $all_promotions, 'ID' );

// Do a new query with these IDs to get a properly-sorted list of promotions
$promotions = get_posts( array(
    'post__in'    => $promotion_ids,
    'post_type'   => 'promotion',
    'post_status' => 'publish'
) );

// Now loop through your promotions
foreach( $posts as $post ) :   
    setup_postdata($post); ?>

<li>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <br/>
    <span>
        <?php
        $meta = get_post_meta(get_the_ID(), 'sp_endDate', true);
        if ( '' != $meta ) {
            $formattedDate = new DateTime( $meta );
            echo "Expires: ".$formattedDate->format('F d, Y');
        }
        ?>
    </span>
</li>
<?php endforeach; ?>
  • Thanks for your help so far, your "very rough pseudocode" seems pretty close, when I print the array for $promotion_ids, it's listing the correct posts that should display, but nothing displays, I've created a pastebin to show where the code stands currently. Thanks again. pastebin.com/pZkQCf9y – Chuck Apr 10 '12 at 17:27
  • I was missing 'post_type' => 'promotions' in $promotions, thanks for your earlier efforts. – Chuck Apr 11 '12 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.