• I’m trying to get separate my search results by specific categories. So for instance if I were to type “search item”. I would receive a results page like the following.

    5 results for “search item”

    Videos (2)
    – Result 1
    – Result 2

    Images (3)
    – Result 1
    – Result 2

    I’m using the following code to attempt to achieve this

    <h1>Search: <?php echo $wp_query->found_posts; ?> results for "<?php echo wp_specialchars($s); ?>"</h1>
    
    <?php query_posts($query_string.'&cat='.$theme_motionstok_cat); if (have_posts()) : ?>
    	<?php include('search-content.php'); ?>
    <?php endif; wp_reset_query(); ?>
    
    <div class="clear"></div>
    
    <?php query_posts($query_string.'&cat='.$theme_photostok_cat); if (have_posts()) : ?>
    	<?php include('search-content.php'); ?>
    <?php endif; wp_reset_query(); ?>
    
    <div class="clear"></div>

    The problem is the category variable in query_posts seems to be ignored and every loop returns the same search results. Any help would be appreciated.

Viewing 15 replies - 1 through 15 (of 17 total)
  • I see what you’re trying to do, but what if one of those posts was in both categories?…

    Thread Starter GhostPool

    (@ghostpool)

    t31os_, no post will ever be in one of the other categories, it’s just assigned to the one category.

    It’s not always going to be reliable to use query_string to preserve parameters, where possible switch for an array of args and merge said array with $wp_query->query …

    rewind_posts() will allow you to roll back the query (so you can iterate over it again) … All you need is to is loop over the query for each category..

    If you’ve not made any progess by yourself later today i’ll post some “help” code up for you… ??

    Thread Starter GhostPool

    (@ghostpool)

    Hi t31os_,

    I’ve tried incorporating what you’ve said (although I may have done it wrongly) and still no luck. I really hope hope you can help me out.

    <?php
    rewind_posts();
    $args = array(
    'cat' => $theme_motionstok_cat,
    );
    $wp_query->query = new WP_Query($args); ?>
    <h2 class="search-title"><?php single_cat_title(); ?> (<?php echo $wp_query->found_posts; ?>)</h2>
    <div class="search-content">
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <?php include('search-content.php'); ?>
    <?php endwhile; ?>
    </div>

    I hadn’t forgotten.. ??

    I did realise, that it’s actually a little tougher then what i originally thought it to be.

    Spent 10 minutes fiddling with the default (default theme), search.php, and managed to get a copy working…

    You’ll want to adjust and toy with it i’m sure, and it can be expanded a little more, i just wanted to get a working copy available…

    See how you get on with this.
    https://wordpress.pastebin.ca/1732276

    Search results will now group by category.

    Thread Starter GhostPool

    (@ghostpool)

    I appreciate the help t31os_. ??

    Before I play around with the code. Would it be possible to achieve something like this: https://www.ghostpool.com/customs/?s=lorem

    1) I only want specific categories to be displayed in the search results e.g. videos, photos, fonts.

    2) Assign a category title in between each group of results.

    3) Style each group of results differently e.g. video category search results have thumbnail images and font search results just display text.

    The category ID is output as part of the wrapping DIV element, if you want to use that to do other category stuff you can..

    $cat_id in this case..

    Take this area for example:

    foreach( $category as $cat_id => $items ) {
    			?>
    
    			<div class="search-category-<?php echo $cat_id; ?>">
    
    			<?php

    Let’s say you wanted to add a condition in there that did different things for different categories, what i’d do is use a switch like so..

    foreach( $category as $cat_id => $items ) {
    			switch( $cat_id ) {
    				case 3;
    				// It's cat with the ID of 3
    				// do something
    				break;
    				case 5:
    				// It's cat with the ID of 5
    				// do something
    				break;
    				default:
    				// It's none of the above so do this
    
    				break;
    			}
    			?>
    
    			<div class="search-category-<?php echo $cat_id; ?>">
    
    			<?php

    Or if you wanted to grab some more data about the category, then you can add some additional bits..
    For example:

    foreach( $category as $cat_id => $items ) {
    
    	$the_cat = get_the_category ( $cat_id );
    	// $the_cat is now array of category objects
    	/*
    	Example of $the_cat - contents (in this case there's only 1 category)..
    
    	Array
    	(
    		 [0] => stdClass Object
    			  (
    					[term_id] => 12
    					[name] => mycategory
    					[slug] => mycatslug
    					[term_group] => 0
    					[term_taxonomy_id] => 123
    					[taxonomy] => category
    					[description] =>
    					[parent] => 0
    					[count] => 0
    					[cat_ID] => 12
    					[category_count] => 0
    					[category_description] =>
    					[cat_name] => mycategory
    					[category_nicename] => mycat
    					[category_parent] => 0
    			  )
    
    	)
    
    	$the_cat[0] would be the first category in the returned array
    	$the_cat[0]->name would output the name
    
    	Would give more examples, but it's not going to help much if you don't understand objects...
    	*/
    
    ... the other code (trimmed for illustration)

    Does that help?

    Thread Starter GhostPool

    (@ghostpool)

    I’ve finally got around to trying the code out. I’ve had no luck getting it to work, php really is not my strong point.

    Thanks for the help though t31os_. It’s appreciated. I guess I’ll have to drop this idea for the search page.

    t31os_,

    Thank you so much for your code. I tried it as it is and it worked perfectly. I just need to do some adjustments to get posts’ stuff display like previously, but this shouldn’t be a problem.

    However, I can’t get the category name above each group of results. I’m not a coder, I rely usually on my common sense, but here I just can get what I want…

    How — or rather where — exactly shall I use the $the_cat[0]->name? This is what I did, and obviously it doesn’t work:

    . . .
    
    foreach( $category as $cat_id => $items ) {
    	$the_cat = get_the_category ( $cat_id );
    	// $the_cat is now array of category objects
    	?>
    
    	<div class="search-category-<?php echo $cat_id; ?>">
    	<?php echo $the_cat[0]->name; ?>
    	<?php
    	// For each item in this category array
    	foreach( $items as $post ) :
    
    . . .

    Once again thanks for your precious help

    In place of ..

    <?php echo $the_cat[0]->name; ?>

    Please insert the following..

    If a test site

    <pre><?php print_r( $the_cat ); ?></pre>

    If production site

    <!--
    <pre><?php print_r( $the_cat ); ?></pre>
    -->

    The output will go into the source of the page in the second example (so your users don’t see it).

    Please provide the output … so i can check you’re getting the data you should be..

    Thanks a lot for your quick reply! Unfortunately this code only returns the following (page source):

    <pre>Array
    (
    )
    </pre>

    Off to bed now, but to respond quickly..

    That’s an empty array, so it means this line is not getting any data back..

    $the_cat = get_the_category ( $cat_id );

    I managed to solve my problem. The array was empty because the ‘get_the_category’ function requires a post ID as the parameter, not a category ID.

    The function to be used here in order to retrieve the category’s name is actually ‘get_cat_name’. Using the following works perfectly:

    $the_cat = get_cat_name ( $cat_id );

    Once again thanks a lot for your precious time.

    It’s sometimes quite late when i write, so please forgive minor mistakes like that… ??

    Had a feeling it would be something simple but was obviously wanting to catch some precious zzz’s, but happy to hear you found (and resolved) the problem.

    ??

    Hellow
    Im not speak english so sorry, but I have a solution if you have a few categories.
    In the blog I’m working I have two categories that would like to display the search FILMS and BLOG, I’ll paste the code, hope that helps.
    Then tell me if it worked

    <?php if (have_posts()) : ?>
    			<h3><?php printf( __( 'Search Results for: %s'), '<span>' . get_search_query() . '</span>' ); ?></h3>
    		<?php endif;?>
    			<?php query_posts('category_name=blog'); ?>
    				<?php if (have_posts()) : ?>
    					<?php $blogResults=0; ?>
    				<?php while (have_posts()) : the_post(); ?>
    					<?php
    						$blogResults++;
    					?>
    				<?php endwhile; ?>
    					<h3><?php echo $blogResults; ?> Results in BLOG</h3>
    					<?php while (have_posts()) : the_post(); ?>
    					<div class="films">
    						<div class="thumb">
    							<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
    						</div>
    						<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    						<div class="entry">
    							<?php the_excerpt() ?>
    						</div>
    					</div>
    					<?php endwhile; ?>
    				<?php endif;?>
    				<?php query_posts('category_name=films'); ?>
    				<?php if (have_posts()) : ?>
    					<?php $fimlsResults=0; ?>
    				<?php while (have_posts()) : the_post(); ?>
    					<?php
    						$filmsResults++;
    					?>
    				<?php endwhile; ?>
    					<h3><?php echo $filmsResults; ?> Results in Films</h3>
    					<?php while (have_posts()) : the_post(); ?>
    					<div class="films">
    						<div class="thumb">
    							<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
    						</div>
    						<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    						<div class="entry">
    							<?php the_excerpt() ?>
    						</div>
    					</div>
    					<?php endwhile; ?>
    				<?php endif;?>

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Separate search results by category’ is closed to new replies.