Forums

Show Search Result Count in Homepage outside of WordPress (3 posts)

  1. freese
    Member
    Posted 2 years ago #

    Hello everybody,

    I have the following problem:

    In a Homepage that is outside of wordpress (another domain) I hve includeda searching functionality to other pages, more or less like a"One-Homepage-shows-you-the-search-result-count-of-different-blogs".

    Something like the script beeing sowed here:

    http://wordpress.org/support/topic/226572

    What script would I need to use to be able to query wp-Database and return just the search result count and not only the number of all posts like in the script above? That's it.

    Thank you very much!!

  2. freese
    Member
    Posted 1 year ago #

    Hello community:
    I have a short question regarding showing information from WordPress outside of the WordPress installation: For a project at the company I work, I need a Script that shows up the search result count of a wordpress blog, outside of it. What I mean is a separate website, that is used as a metasearch engine (searches in different blogs), I have to show the search result number from a wordpress blog. There are different approaches that I have in mind. I could maybe use Tip Nr. 8 from this Smashing Magazine Article:

    http://www.smashingmagazine.com/2009/06/10/10-useful-wordpress-loop-hacks/

    which means creating my own WordPress Loop using The WP_Query Object in a File at the root folder and then inlude this php file in the separate website? or as a second approach I could try and modify this script from the wordpress forum:

    http://wordpress.org/support/topic/226572?replies=15

    so that it pulls, not the the number of posts in the WP database but the search results count. Only I don’t know how to achieve it.

    Of course the query:

    <?php
    // ...Call the database connection settings
    require( path to /wp-config.php );
    
    // ...Connect to WP database
    $dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if ( !$dbc ) {
        die( 'Not Connected: ' . mysql_error());
    }
    // Select the database
    $db = mysql_select_db(DB_NAME);
    if (!$db) {
        echo "There is no database: " . $db;
    }
    
    // ...Formulate the query
    $query = "
        SELECT *
        FROM <code>wp_posts</code>
        WHERE <code>post_status</code> = 'publish'
        AND <code>post_password</code> = ''
        AND <code>post_type</code> = 'post'
        ";
    
    // ...Perform the query
    $result = mysql_query( $query );
    
    // ...Check results of the query and terminate the script if invalid results
    if ( !$result ) {
        $message = '<p>Invalid query.</p>' . "\n";
        $message .= '<p>Whole query: ' . $query ."</p> \n";
    	die ( $message );
    }
    
    // Init a variable for the number of rows of results
    $num_rows = mysql_num_rows( $result );
    
    // Print the number of posts
    echo "$num_rows Posts";
    
    // Free the resources associated with the result set
    if ( $result ) {
        mysql_free_result( $result );
        mysql_close();
    }
    ?>

    hast to be different, but I´m really not a php master. Thank you very much for your support! Thanks a lot!

  3. farfromfearless
    Member
    Posted 1 year ago #

    Wow. It's a lot simpler than that to count WordPress' search results, here:

    <?php
        global $wp_query;
        $count = sizeof( $wp_query->posts );
        echo"<pre>";
        //var_dump($wp_query);// The WP Query Object
        var_dump($wp_query->posts); // The WP Query Object's 'posts' property (note the plural)
        echo"</pre>";
    ?>

    If you drop that snippet in your search template, it will output the properties of the '$wp_query' variable, specifically the 'posts' property of the object. This is an array that stores a collection of the returned posts from the search query, which you can do a number of things with once you access it, including (*drumroll*), *count* the number of posts in that array using either PHP's 'count()' or 'sizeof()' function.

Topic Closed

This topic has been closed to new replies.

About this Topic