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.

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 { }
        ?>
share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

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

share|improve this answer
    
Hi apologies for the late response. I did try this code but it added the title to every result returned. –  user1888564 Jun 19 '13 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 Jun 19 '13 at 13:56
    
Hi I managed to get this in the loop, Thanks ever so much - worked like a charm! –  user1888564 Jun 25 '13 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. –  user1888564 Jun 25 '13 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 Jun 25 '13 at 13:14
add comment

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).

share|improve this answer
    
In search.php I have: <?php /* Start the Loop */ ?> <?php while ( have_posts() ) : the_post(); ?> <?php get_template_part( 'content', 'search' ); ?> –  user1888564 Jun 18 '13 at 9:11
    
I want the title to be added to each post type. –  user1888564 Jun 18 '13 at 9:30
add comment

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.