This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

merge queries

By: cw17s0n on Apr 10th, 2012  |  syntax: PHP  |  size: 1.52 KB  |  views: 106  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. $ongoing_args = array(
  3.         'post_type' => 'promotions',
  4.         'meta_key' => 'sp_ongoingPromotion',
  5.         'meta_value' => 1
  6. );
  7.  
  8. $current_args = array(
  9.         'post_type' => 'promotions',
  10.         'meta_key' => 'sp_endDate',
  11.         'meta_value' => date("Y/m/d"),
  12.         'meta_compare' => '>=',
  13.         'orderby' => 'meta_value',
  14.         'order' => 'ASC'
  15. );
  16.  
  17. // Get promotions using the arguments outlined above.
  18. $ongoing_promotions = get_posts( $ongoing_args );
  19. $current_promotions = get_posts( $current_args );
  20.  
  21. // Merge arrays
  22. $all_promotions = array_merge( $ongoing_promotions, $current_promotions );
  23.  
  24. // Get just the ID of promotions
  25. $promotion_ids = wp_list_pluck( $all_promotions, 'ID' );
  26. print_r($promotion_ids);
  27.  
  28. // Do a new query with these IDs to get a properly-sorted list of promotions
  29. $args = array(
  30.         'post__in' => $promotion_ids,
  31.         'numberposts' => 5,
  32.         'post_status' => 'publish'
  33. );
  34. $wp_query= null;
  35. $wp_query = new WP_Query();
  36. $wp_query->query( $args );
  37.  
  38.                 $wp_query = new WP_Query($args);
  39.                 if($wp_query->have_posts()){
  40.                     while($wp_query->have_posts()){
  41.                         $wp_query->the_post();
  42.     ?>
  43.  
  44. <li>
  45.     <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
  46.     <br/>
  47.     <span>
  48.         <?php
  49.         $meta = get_post_meta(get_the_ID(), 'sp_endDate', true);
  50.         if ( '' != $meta ) {
  51.             $formattedDate = new DateTime( $meta );
  52.             echo "Expires: ".$formattedDate->format('F d, Y');
  53.         }
  54.         ?>
  55.     </span>
  56. </li>
  57. <?php }
  58. } else { ?>
  59.         <p>No Posts were found</p>
  60. <?php }
  61. wp_reset_postdata();
  62. ?>
clone this paste RAW Paste Data