0

I have a search page that is the code below, it returns different post types based upon a user search, Is there a way I can add the following title to each post type that is found - everything I've tried only adds it to every item (I'm guessing its the loop).

Custom post type 1 results - title 1 that I need to add

--- Results 1 found here

Custom post type 2 results - title 2 that I need to add

--- Results 2 found here

Here is my current code:

 <?php  if (is_search()) { ?>
    <?php if (get_post_type() == 'type_1') {
        //Style type_1 ?>
    <div id="type_1" <?php post_class(); ?>>    
  <div id="file-loader"> <a href="<?php echo get_content_link( get_the_content() ); ?>">
            <?php the_post_thumbnail(180, 230); ?>
    </a> 
  </div>
  <div id="file-hover"></div>
  <h4>
    <a href="<?php echo get_content_link( get_the_content() ); ?>"><?php the_title(); ?></a>
  </h4>
</div>
    <?php   
    } else if (get_post_type() == 'type_2') {
        //Do different styling
    }//endif
    else { }
        ?>

2 Answers 2

0

For this to work, you will need to have your search results ordered by the post type.

function order_by_pt($where,$qry) {
  if (is_main_query() && $qry->is_search()) {
    global $wpdb;
    $where = $wpdb->posts.'.post_type DESC';
  }
  return $where;
}
add_action('posts_orderby','order_by_pt',1,2);

Then a Loop like this one will do what you want, if I understand you.

$type_title = '';
while (have_posts()) { 
  the_post();
  // this is where the title is checked and printed
  if (get_post_type() !== $type_title) {
    $type_title = get_post_type();
    echo '<h2>Custom Post Type "'.$type_title.'" Results</h2>';
  }
  // switches are neater than a bunch of if/ifelses :)
  switch (get_post_type()) {
    case 'type_1' :
      //Style type_1 ?>
      <div id="type_1" <?php post_class(); ?>>    
        <div id="file-loader">
          <a href="<?php echo get_content_link( get_the_content() ); ?>">
            <?php the_post_thumbnail(180, 230); ?>
          </a> 
        </div>
        <div id="file-hover"></div>
          <h4>
            <a href="<?php echo get_content_link( get_the_content() ); ?>"><?php the_title(); ?></a>
          </h4>
      </div><?php
    break;
    case 'type_2' :
      //Do different styling
      echo '<p>',the_title(),'</p>';
    break;
    case 'some-other-type' :

    break;
  }
}

I formatted your code, put it is a Loop, and used a switch instead of an if/else chain.

That code should produce output like:

enter image description here

5
  • Hi apologies for the late response. I did try this code but it added the title to every result returned. Commented Jun 19, 2013 at 8:49
  • @user1888564 : I altered the order_by_pt callback and hooked it to posts_orderby and added a screenshot of the output. What you see is a search for ?s=image with theme test data and a book post type.
    – s_ha_dum
    Commented Jun 19, 2013 at 13:56
  • Hi I managed to get this in the loop, Thanks ever so much - worked like a charm! Commented Jun 25, 2013 at 12:23
  • Spoke too soon. The search title is appearing under every result returned - could it be because the check for the results is happening within the loop and as such returning the type title for each result. Commented Jun 25, 2013 at 12:47
  • Tested again. This works when I try it. You must be doing something wrong but I have no way tell what.
    – s_ha_dum
    Commented Jun 25, 2013 at 13:14
0

The current code you posted here, is only inside part of the loop.

If you want to add title on top of the search results list, you have to find template part that is showing this title. (It should be search.php file of your theme, but it depends on theme structure).

2
  • In search.php I have: <?php /* Start the Loop */ ?> <?php while ( have_posts() ) : the_post(); ?> <?php get_template_part( 'content', 'search' ); ?> Commented Jun 18, 2013 at 9:11
  • I want the title to be added to each post type. Commented Jun 18, 2013 at 9:30

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.