categories - Search by tag, category and author without plugin

admin2025-01-07  5

I'm creating a website that has search feature. I am wondering what should i do in order to make the search box works like search everything. The search term that visitor inserted may be hitting the tag name of the post, category of the post, content inside post or title of the post. As long as there is a word within the post hit by search term, the result will show. I'm not going to use plugin, or to add radio button to my search form. I tried to search around but failed. Any suggestion and solution?

<!DOCTYPE html>
<html>
<?php get_header(); ?>
<body>

<?php $q1 = get_posts(array(
    'post_status' => 'publish',
    'posts_per_page' => '5',
    's' => get_search_query()
    )
);

$q2 = get_posts(array(
    'post_status' => 'publish',
    'posts_per_page' => '5',
    'tax_query' => array(
            //your query
        )
    )
);

$merged = array_merge($q1, $q2);

?>

<?php echo wp_specialchars($s); ?><br/>
<?php the_search_query(); ?><br/>
<?php the_search_query(); ?>
<br/>
<article>
<p>You have searched for "<?php echo wp_specialchars($s); ?>". We found <?php /* Search Count */ 
    $allsearch = &new WP_Query("s=$s&showposts=-1"); 
    $key = wp_specialchars($s, 1); 
    $count = $allsearch->post_count; 
    $text  = '<span class="resultsFounds">';
if ( $allsearch->found_posts <= 0 ) {
    $text .= sprintf(__( 'no company' ), $count );
}

elseif ( $allsearch->found_posts <= 1 ) {
    $text .= sprintf(__( '%d related company' ), $count );
} 

else {
    $text .= sprintf(__( '%d related companies' ), $count );
}       
$text .= '</span>'; 
echo $text;
?> with the keyword you searched for. If the results are not what you expected, we suggest you to try for different keywords which related to the company.</p>
<?php if (have_posts()) : ?>
<h2>Keywords : <?php echo wp_specialchars($s); ?><?php 
    /* Search Count */
    $count = $wp_query->found_posts; 
    $text  = '<span class="resultsFound">';
if ( $count  <= 0 ) {
    $text .= sprintf(__( '( no company )' ), $count );
}

elseif ( $count <= 1 ) {
    $text .= sprintf(__( '( %d company )' ), $count );
} 

else {
    $text .= sprintf(__( '( %d companies )' ), $count );
}       
$text .= '</span>'; 
echo $text;
?></h2>

<?php include("adsRandom.php"); ?>
<?php include("boostBiz/bizAds.php"); ?>

<?php while (have_posts()) : the_post(); ?>
<div class="ncc <?php the_ID(); ?><?php if( date('U') - get_the_time('U', $post->ID) < 24*60*60 ) : ?> new<?php endif; ?><?php if (is_sticky()) { ?> sponsored<?php } ?>" <?php if (is_sticky()) { ?>title="Our Advertiser"<?php } ?>>
<h3 class="excerpt"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php search_title_highlight(); ?></a></h3>
<?php search_excerpt_highlight(); ?>
<p class="excerptInfo"><?php printf( __( 'Listed in %2$s', 'NCC' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?> |<?php
$tags_list = get_the_tag_list( '', ', ' );
if ( $tags_list );
?>
<?php printf( __( ' Located In: %2$s', 'NCC' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); ?><?php if( date('U') - get_the_time('U', $post->ID) < 24*60*60 ) : ?> | Published <?php echo get_the_date(); ?><?php endif; ?></p>
</div><!--ncc <?php the_ID(); ?>-->

<?php endwhile; ?>
<?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>

<?php else : ?>

<h2><?php _e('Keywords : ','NCC'); ?><?php echo wp_specialchars($s); ?><?php /* Search Count */ 
    $allsearch = &new WP_Query("s=$s&showposts=-1"); 
    $key = wp_specialchars($s, 1); 
    $count = $allsearch->post_count; 
    $text  = '<span class="resultsFound">';
if ( $allsearch->found_posts <= 0 ) {
    $text .= sprintf(__( '( Nothing Found )' ), $count );
}

elseif ( $allsearch->found_posts <= 1 ) {
    $text .= sprintf(__( '( We found %d company )' ), $count );
} 

else {
    $text .= sprintf(__( '( We found %d companies )' ), $count );
}       
$text .= '</span>'; 
echo $text;
?></h2>

<article class="nccSingle">
<p>Your search - "<b><?php echo wp_specialchars($s); ?></b>" - did not match any documents. Possibly, there is no company listed with this keyword. Or, the inserted keyword was wrong in spelling?</p>
<p><b>Suggestions:</b></p>
<ul>
  <li>Make sure all words are spelled correctly.</li>
  <li>Try different keywords.</li>
  <li>Try more general keywords.</li>
</ul>
</article>
<?php endif; ?>
</body>
</html>

I'm creating a website that has search feature. I am wondering what should i do in order to make the search box works like search everything. The search term that visitor inserted may be hitting the tag name of the post, category of the post, content inside post or title of the post. As long as there is a word within the post hit by search term, the result will show. I'm not going to use plugin, or to add radio button to my search form. I tried to search around but failed. Any suggestion and solution?

<!DOCTYPE html>
<html>
<?php get_header(); ?>
<body>

<?php $q1 = get_posts(array(
    'post_status' => 'publish',
    'posts_per_page' => '5',
    's' => get_search_query()
    )
);

$q2 = get_posts(array(
    'post_status' => 'publish',
    'posts_per_page' => '5',
    'tax_query' => array(
            //your query
        )
    )
);

$merged = array_merge($q1, $q2);

?>

<?php echo wp_specialchars($s); ?><br/>
<?php the_search_query(); ?><br/>
<?php the_search_query(); ?>
<br/>
<article>
<p>You have searched for "<?php echo wp_specialchars($s); ?>". We found <?php /* Search Count */ 
    $allsearch = &new WP_Query("s=$s&showposts=-1"); 
    $key = wp_specialchars($s, 1); 
    $count = $allsearch->post_count; 
    $text  = '<span class="resultsFounds">';
if ( $allsearch->found_posts <= 0 ) {
    $text .= sprintf(__( 'no company' ), $count );
}

elseif ( $allsearch->found_posts <= 1 ) {
    $text .= sprintf(__( '%d related company' ), $count );
} 

else {
    $text .= sprintf(__( '%d related companies' ), $count );
}       
$text .= '</span>'; 
echo $text;
?> with the keyword you searched for. If the results are not what you expected, we suggest you to try for different keywords which related to the company.</p>
<?php if (have_posts()) : ?>
<h2>Keywords : <?php echo wp_specialchars($s); ?><?php 
    /* Search Count */
    $count = $wp_query->found_posts; 
    $text  = '<span class="resultsFound">';
if ( $count  <= 0 ) {
    $text .= sprintf(__( '( no company )' ), $count );
}

elseif ( $count <= 1 ) {
    $text .= sprintf(__( '( %d company )' ), $count );
} 

else {
    $text .= sprintf(__( '( %d companies )' ), $count );
}       
$text .= '</span>'; 
echo $text;
?></h2>

<?php include("adsRandom.php"); ?>
<?php include("boostBiz/bizAds.php"); ?>

<?php while (have_posts()) : the_post(); ?>
<div class="ncc <?php the_ID(); ?><?php if( date('U') - get_the_time('U', $post->ID) < 24*60*60 ) : ?> new<?php endif; ?><?php if (is_sticky()) { ?> sponsored<?php } ?>" <?php if (is_sticky()) { ?>title="Our Advertiser"<?php } ?>>
<h3 class="excerpt"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php search_title_highlight(); ?></a></h3>
<?php search_excerpt_highlight(); ?>
<p class="excerptInfo"><?php printf( __( 'Listed in %2$s', 'NCC' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?> |<?php
$tags_list = get_the_tag_list( '', ', ' );
if ( $tags_list );
?>
<?php printf( __( ' Located In: %2$s', 'NCC' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); ?><?php if( date('U') - get_the_time('U', $post->ID) < 24*60*60 ) : ?> | Published <?php echo get_the_date(); ?><?php endif; ?></p>
</div><!--ncc <?php the_ID(); ?>-->

<?php endwhile; ?>
<?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>

<?php else : ?>

<h2><?php _e('Keywords : ','NCC'); ?><?php echo wp_specialchars($s); ?><?php /* Search Count */ 
    $allsearch = &new WP_Query("s=$s&showposts=-1"); 
    $key = wp_specialchars($s, 1); 
    $count = $allsearch->post_count; 
    $text  = '<span class="resultsFound">';
if ( $allsearch->found_posts <= 0 ) {
    $text .= sprintf(__( '( Nothing Found )' ), $count );
}

elseif ( $allsearch->found_posts <= 1 ) {
    $text .= sprintf(__( '( We found %d company )' ), $count );
} 

else {
    $text .= sprintf(__( '( We found %d companies )' ), $count );
}       
$text .= '</span>'; 
echo $text;
?></h2>

<article class="nccSingle">
<p>Your search - "<b><?php echo wp_specialchars($s); ?></b>" - did not match any documents. Possibly, there is no company listed with this keyword. Or, the inserted keyword was wrong in spelling?</p>
<p><b>Suggestions:</b></p>
<ul>
  <li>Make sure all words are spelled correctly.</li>
  <li>Try different keywords.</li>
  <li>Try more general keywords.</li>
</ul>
</article>
<?php endif; ?>
</body>
</html>
Share Improve this question edited Feb 8, 2017 at 8:04 Jornes asked Feb 8, 2017 at 5:30 JornesJornes 7535 gold badges12 silver badges31 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

I can think of two options:

  1. Multiple WP_Queries, each for normal search, taxonomies and meta. And merge the results you get from each.

OR

  1. A WPDB query, with join between _post, _postmeta, _terms(and all tables related to terms).

Although I will prefer 1st method, it's your choice what you are comfortable with and performance you are expecting, in term of resource ans speed.

Edit:

$q1 = get_posts(array(
    'post_status' => 'publish',
    'posts_per_page' => '-1',
    's' => get_search_query()
    )
);

$q2 = get_posts(array(
    'post_status' => 'publish',
    'posts_per_page' => '-1',
    'tax_query' => array(
            //your query
        )
    )
);

$merged = array_merge($q1, $q2);

Not exact query but just to give you an idea. Please check for any syntax errors as I haven't tested it.

$query = new WP_Query( array(<br/>
'post_type' => 'post',<br/>
'author' => 'author-id or user_nicename',<br/>
'tax_query' => array(<br/>
        'relation' => 'OR',<br/>
        array(<br/>
            'taxonomy' => 'category',<br/>
            'field'    => 'slug',<br/>
            'terms'    => array( 'edge-case-1', 'edge-case-2' ),<br/>
        ),<br/>
        array(<br/>
            'taxonomy' => 'post_tag',<br/>
            'field'    => 'term_id',<br/>
            'terms'    => array( 66, 67 ),<br/>
        ),<br/>
    ),<br/>

 ) );<br/>
if ( $query->have_posts() ) {<br/>
    while ( $query->have_posts() ) {<br/>
        $query->the_post();<br/>

        the_title();
<br/>
        the_content();
<br/>
    }<br/>
    wp_reset_postdata();
<br/>
}<br/>

For author you can pass either author Id or author's user_nicename. And in tax_query parameter for 'field' you can use either 'term_id' or 'slug' and 'terms' depends what you pass in 'field' parameter as shown in above. In 'terms' array or single variable can be passed.enter code here

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736260748a687.html

最新回复(0)