0

I'm trying to first fetch latest 18 post from all of our custom post_types (4) sorted on a custom field in DESC and then display them in sequence.

E.g I have custom post_types: paper, policy, media and Event (Event type: meeting, conference, and seminars).

The order of display required is 1st post will be from paper, 2nd from policy, 3rd from media, 4th from event but it will be a conference, 5th will be again paper, 6th policy etc. So, in a way below is the sequence of post required.

paper - 1, 5, 9
policy - 2,6,10
media - 3,7,11
event {conference} - 4,13,16
event {meeting} - 8,14,17
event {seminar} - 12,15,18

The code used is below:

<?php
      $counter = 1;

      $args = array(
  'post_type'   => 'any',
  'post_status' => 'publish',
  'posts_per_page' => '18',
          'meta_key'    => 'first_day',
           'orderby' => 'meta_value',
         'order' => 'DESC'
 );

$query = new WP_Query( $args );

      if( $query->have_posts() ) :


while( $query->have_posts() ) : $query->the_post();

if ($counter ==1 || $counter ==5 || $counter ==9) 
{
//display paper post data
}

if ($counter ==2 || $counter ==6 || $counter ==10) 
{
//display policy data
}

if ($counter ==3 || $counter ==7 || $counter ==11) 
{
//display media post detail
}

if ($post->post_type == "event") 
{
  if($slug==meetings &&  $counter ==8 || $counter ==14 || $counter ==17) 
   {
    //display meeting type event detail
   }

   if($slug==seminars &&  $counter ==12 || $counter ==15 || $counter ==18) 
     {
      //display seminar eventtype post detail here
     }

     if($slug==conferences &&  $counter ==4 || $counter ==13 || $counter ==16) 
     { 
      //display conference event type post detail
     }

 $counter++
 endwhile;
 wp_reset_postdata();

?>

The problem is that I'm able to display paper/policy and media in sequence but not for the Event custom post_type as in the loop some of the post type do not display. If I echo $counter and custom post type details I get it like below:

1Paper:1 2Policy:2 3commentary:3 4 5Paper:5 6Policy:6 7commentary:7 8 9Paper:9 10Policy:10 11commentary:11 12 13 14 15 Seminar15 16 17 Meeting17 18

If you notice above, 4th, 8th, 12,13,14,15,17 and 18th post doesn't display which are mainly conference/meeting/seminar 'eventtype'.

Please let me know what is wrong in the code.

  • This is quite complicated. Are you trying to get them in this order for presentational reasons? You might be better off just making separate queries. – Jacob Peattie Jul 10 '18 at 8:18
  • Yes, for presentation. I'm developing the homepage which will show mix of all the post in sequence explained above. I could do separate query but the order of sequence will be there? – Rakesh Ojha Jul 10 '18 at 10:12
  • If you do separate queries the sequence can be whatever you want it to be. Just query what you need in each spot. – Jacob Peattie Jul 10 '18 at 10:24
  • Will I do separate query within a loop with incremental counter to check the order/sequence of each post? Could you help in this direction a bit? – Rakesh Ojha Jul 10 '18 at 10:53

Your Answer

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

Browse other questions tagged or ask your own question.