0

i've done a fair look round before posting this and i'm not getting much luck. Here's the scenario. I have a search query which retrieves normal posts and two additional custom post types (distributors wanted and events), and displays the results separated into their own divs using a foreach loop, thusly:

if( have_posts() ){
$types = array('post', 'tribe_events', 'distribute');
$counter = 1;
foreach( $types as $type ){
    echo '<div class="results ';
    echo $type. '">';
    if($type == 'post'){
        echo '<h4><i class="fa fa-newspaper-o"></i> Articles</h4>';
    }
    if($type == 'distribute'){
        echo '<h4><i class="fa fa-truck"></i> Distributors Wanted</h4>';
    }
    if($type == 'tribe_events'){
        echo '<h4 class="events">Events</h4>';
    }
    echo '<ul class="looplist">';
    while( have_posts() ){
        the_post();
        if( $type == get_post_type() ){
            get_template_part('parts/search', $type);
        }
        $counter++ ;
    }
    rewind_posts();

    echo '</ul></div>';
}

This is all working ok, retrieving the selected parts, but there's two issues i'm not sure how to address. 1: because it's sorting results by the post date ascending (default) it's not really working well for the events (which are being handled by 'the events calendar' by Modern Tribe)- since they're published at arbitrary dates as listings come in, but have a specific meta value for their start dates, which i would like to order them by if possible. i've used the query for this in a normal WP_query like so:

'meta_key'=>'_EventStartDate',
  'orderby'=>'_EventStartDate',
  'order'=>'ASC',

2: since the code is using a foreach loop, i'm not sure how to approach hiding empty results, which means even if no results are returned for the post type in question it still renders the heading and container div.

Can anyone suggest a solution? i did onder about conditionally passing $args to each post type, but i tried a few things and all i got was a headache and a white screen.. apologies if my lack of knowledge is a hindrance here i am a relative newbie! Cheers! Pete

1 Answer 1

0

The below should loop your post types and only insert the header once if it has a post for that post type. I am sure there is a lot better ways to code this cleaner but this should work * I have not tested only coded in this page.

I am not sure about the ordering of the custom post type attribute as I do not know that plugin and your query looks to be correct might be worth trying without the _ as I have done this in Advanced Custom Fields and did not require the _ .

would be worth checking your database as I know advanced custom fields saves dates like this 03212017 and Wordpress might not be able to see this as a valid date.

  if (have_posts()) :


    $counter = 1;
    $post_counter_1 = 0;
    $post_counter_2 = 0;
    $post_counter_3 = 0;


    while ( have_posts() ) : the_post();

    // Get post type to display
    if(get_post_type() == 'post'){
        // run once for creating header
       if($post_counter_1 == 0)
       {
        $post_counter_1 = 1;
        echo '<h4><i class="fa fa-newspaper-o"></i> Articles</h4>';
       }
       echo '<div class="results post">';
       get_template_part('parts/search', 'post');

    }
endwhile;
while ( have_posts() ) : the_post();
    if(get_post_type() == 'tribe_events'){

       if($post_counter_2 == 0)
       {
         $post_counter_2 = 1;
         echo '<h4><i class="fa fa-truck"></i> Distributors Wanted</h4>';
       }
       echo '<div class="results tribe_events">';
       get_template_part('parts/search', 'tribe_events');
    }
endwhile;

while ( have_posts() ) : the_post();
    if(get_post_type() == 'distribute'){

       if($post_counter_3 == 0)
       {
         $post_counter_3 = 1;
         echo '<h4 class="events">Events</h4>';
       }
      echo '<div class="results distribute">';
      get_template_part('parts/search', 'distribute');
    }
endwhile;
endif;
1
  • Thanks Sam- needs some tweaking but i think it's definitely on the right path for hiding the empty headers! Commented Mar 23, 2017 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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