Ask your WordPress questions! Pay money and get answers fast! (more info)

Custom Search results page WordPress

  • SOLVED

Hello !!
I need this scenario in results page ( search.php ), 3 loops on the same page.

Search result for: 'search query'

<div>
- search result only by title of category 1 & 2
</div>

<div>
- search result only by title of category 3 & 4
</div>

<div>
- search result only by content of category 1 & 2 & 3 & 4
</div>


thank you !!

Answers (4)

2014-04-01

Bob answers:

You need filtration based on title and content and category.
It is quite easy to filter post for category using wordpress query but filter using title and content only is little tricky.
Please follow below steps to achieve your desired result.

<strong>Step - 1.</strong>
Add search by title function in your themes <strong>functions.php</strong> (we will use it as filter)
<em>Please be careful it might add extra spaces when copying content form here :)</em>

function __search_by_title_only( $search, &$wp_query )
{
global $wpdb;

if ( empty( $search ) )
return $search; // skip processing - no search term in query

$q = $wp_query->query_vars;
$n = ! empty( $q['exact'] ) ? '' : '%';

$search =
$searchand = '';

foreach ( (array) $q['search_terms'] as $term ) {
$term = esc_sql( like_escape( $term ) );
$search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
$searchand = ' AND ';
}

if ( ! empty( $search ) ) {
$search = " AND ({$search}) ";
if ( ! is_user_logged_in() )
$search .= " AND ($wpdb->posts.post_password = '') ";
}

return $search;
}

<strong>Step -2 .</strong>
Now add function to search content only in your theme's <strong>functions.php</strong> (we will use it as filter)

function __search_by_content_only( $search, &$wp_query )
{
global $wpdb;

if ( empty( $search ) )
return $search; // skip processing - no search term in query

$q = $wp_query->query_vars;
$n = ! empty( $q['exact'] ) ? '' : '%';

$search =
$searchand = '';

foreach ( (array) $q['search_terms'] as $term ) {
$term = esc_sql( like_escape( $term ) );
$search .= "{$searchand}($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
$searchand = ' AND ';
}

if ( ! empty( $search ) ) {
$search = " AND ({$search}) ";
if ( ! is_user_logged_in() )
$search .= " AND ($wpdb->posts.post_password = '') ";
}

return $search;
}


<strong>Step 3</strong>
Now the final code is for <strong>search.php</strong> of your theme.
<em>Please Note:</em> We have to exclude all posts which are already displayed in first loop or second loop so it do not show duplicate results.

global $s;
add_filter( 'posts_search', '__search_by_title_only', 500, 2 );
// The Query for cat 1 and 2
$the_query = new WP_Query( 's='.$s.'&cat=1,2' );
$exclude_ids = array();
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$exclude_ids[] = get_the_ID(); //once post is displayed it should not be visible in next loop so exclude it
echo '<li><a href="'.get_permalink().'">' . get_the_title() . '</a></li>';
}
echo '</ul>';
}
/* Restore original Post Data */
wp_reset_postdata();


// The Query for cat 2 and 3
$args = array('s' => $s, 'category_in' => array(3,4) ,'post__not_in' => $exclude_ids );
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$exclude_ids[] = get_the_ID(); //once post is displayed it should not be visible in next loop so exclude it
echo '<li><a href="'.get_permalink().'">' . get_the_title() . '</a></li>';
}
echo '</ul>';
}
/* Restore original Post Data */
wp_reset_postdata();

// Now we not want to filter using title so remove that filter
remove_filter( 'posts_search', '__search_by_title_only',500);

//filter using content
add_filter( 'posts_search', '__search_by_content_only', 500, 2 );

// The Query for cat 2 and 3
$args = array('s' => $s, 'category_in' => array(1,2,3,4) ,'post__not_in' => $exclude_ids );
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li><a href="'.get_permalink().'">' . get_the_title() . '</a></li>';
}
echo '</ul>';
}
/* Restore original Post Data */
wp_reset_postdata();
remove_filter( 'posts_search', '__search_by_content_only',500 );

Let me know if you have any question.

2014-04-01

Arnav Joy answers:

try this in search.php file

<?php

global $s;

?>

<div>
<?php

query_posts('s='.$s.'cat=1,2');

if( have_posts() ):while(have_posts()):the_post();
the_title();
endwhile;endif;
wp_reset_query();
?>
</div>
<div>
<?php


query_posts('s='.$s.'cat=3,4');

if( have_posts() ):while(have_posts()):the_post();
the_title();
endwhile;endif;
wp_reset_query();
?>
</div>
<div>

<?php


query_posts('s='.$s.'cat=1,2,3,4');

if( have_posts() ):while(have_posts()):the_post();
the_title();
endwhile;endif;
wp_reset_query();
?>
</div>

2014-04-01

zebra webdesigns answers:

Hello manlino

This codex page will help you
https://codex.wordpress.org/Creating_a_Search_Page

after you perform the query using the while loop three times will give the result.
What you need to do is check for whether the the category ID match with the result in each loop


zebra webdesigns comments:

Yep arnav solution is the good one ... but it will take too much time to perform multiple queries.
so better perform one single query for the desired categories and while printing
check for the category condition


Manlio Ma comments:

Hi, Yes, I need the php script..if you can help me...

The arnav code doesn't include results, and when I have write search by title, it means that I need the results only if search keyword is in Title of post, and for last loop , only if it is in Content

2014-04-01

Ryan S answers:

query_posts() is not advisable in creating multiple queries you can use WP_Query http://codex.wordpress.org/Class_Reference/WP_Query instead