Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The result of the query so far is the print of the array, Array ( [0] => 309 [1] => 10 ), 309 & 10 are the correct id's of the posts I want to display, and "No posts were found" the result of hitting "else".

I have a pastebin with the most up to date copy of my code here, http://pastebin.com/pZkQCf9y

Here is the query:

<?php

$ongoing_args = array(
  'post_type' => 'promotions',
  'meta_key' => 'sp_ongoingPromotion',
  'meta_value' => 1
);

$current_args = array(
  'post_type' => 'promotions',
  'meta_key' => 'sp_endDate',
  'meta_value' => date("Y/m/d"),
  'meta_compare' => '>=',
  'orderby' => 'meta_value',
  'order' => 'ASC'
);

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

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

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

// Do a new query with these IDs to get a properly-sorted list of promotions
$args = array(
  'post__in' => $promotion_ids,
  'numberposts' => 5,
  'post_status' => 'publish'
);
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query( $args );

    $wp_query = new WP_Query($args);
    if($wp_query->have_posts()){
        while($wp_query->have_posts()){
            $wp_query->the_post();
?>

Any help greatly appreciated.

share|improve this question
1  
i'm not sure this will help but i'd delete lines 34 - 36. They look to be redundant. what type of post are you querying for (page, post, CPT)? if its a CPT you'll need to add 'post_type' => 'whatever your CPT is' to the $args array. let me know if that helps at all. –  Steve Fischer Apr 11 '12 at 15:47
    
It was the missing 'post_type' I thought having already declared 'post_type' further up I wouldn't need to have done it again. Thanks –  Chuck Apr 11 '12 at 16:04

1 Answer 1

if you set the fields parameter to ids you will get an array of ids instead of unneeded post data ex:

$ongoing_args = array(
  'post_type' => 'promotions',
  'meta_key' => 'sp_ongoingPromotion',
  'meta_value' => 1,
  'fields' => 'ids'
);

$current_args = array(
  'post_type' => 'promotions',
  'meta_key' => 'sp_endDate',
  'meta_value' => date("Y/m/d"),
  'meta_compare' => '>=',
  'orderby' => 'meta_value',
  'order' => 'ASC',
  'fields' => 'ids'
);

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

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

$args = array(
  'post__in' => $promotion_ids,
  'numberposts' => 5,
  'post_status' => 'publish'
);

$all_query = new WP_Query($args);
if($all_query->have_posts()){
    while($all_query->have_posts()){
        $all_query->the_post();
        //loop stuff here
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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