WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Search query and results count not showing in search results (3 posts)

  1. Johnny Bravo
    Member
    Posted 1 year ago #

    Hi guys

    I have google custom search implemented on my blog in a 2-page format, it works OK.

    i'm trying to add the number of posts found, and also the search query from the user to be displayed above my search results, i have tried the following php but it seems to only show '74' results for EVERY search, and the search query itself does not display between the "" speech marks, my search template:

    <?php
    /*
    Template Name: Search Results
    */
    ?>
    <?php
    /**
     * @package WordPress
     * @subpackage Default_Theme
     */
    
    get_header(); ?>
    
    	<div id="content-page">
    <h2>Your Google Search Results</h2>
    <?php
    $mySearch =& new WP_Query("s=$s & showposts=-1");
    $num = $mySearch->post_count;
    echo $num.' useful search results for "'; the_search_query();
    ?>
    
    <!-- Place this tag where you want the search results to render -->
    <gcse:searchresults-only></gcse:searchresults-only>
    <!-- Search Results code above this line -->
    </div>
    <?php
     include
    (
    "sidebar2.php"
    )
    ; ?>
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    i have also tried:

    <?php the_search_query(); ?>

    and

    <?php echo 'Search Results for "'; the_search_query(); echo '"'; ?>

    before my search results, but it doesn't seem to pull the search query in.

    i'm stumped, any gcse experts in the house?
    my blog: http://artbyherbie.com

  2. Noel Forte
    Member
    Posted 1 year ago #

    I was able to figure this out by using a good old little trick in the PHP manual. I did a google search to see if there was any way of echoing out form data that was submitted. It turns out that PHP has that functionality built right in.

    http://php.net/manual/en/reserved.variables.get.php

    The variable $_GET automatically returns the term that is being called for in a defined query. So, if I wanted to echo out my current results, I might create a page title for my page template that looks something like this:

    <h1 class="page-title"><?php echo( 'Search Results for: "'); ?><span><?php echo $_GET['q']; ?></span>"</h1>

    In the example above, we utilized echo $_GET['q']; in our php code, which echoed out the term being currently used by our "q" query term. Notice that to use the quotation marks, I used my double quote in the first echo ('Search Results for: "');, and then closed our quotation marks right before closing the h1 tag.

    Hope this helps. You can see a live demo of this technique in action on my own search results page here.

  3. Johnny Bravo
    Member
    Posted 1 year ago #

    noel, thanks, that worked a charm, i edited your code slightly by taking out the CSS class and using <h3> instead:

    <h3><?php echo( 'You searched for: "'); ?><span><?php echo $_GET['q']; ?></span>"</h3>

    thanks! moving onwards..

Topic Closed

This topic has been closed to new replies.

About this Topic