Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get number of results from query_posts?

Tags:

wordpress

I'm printing posts and I want to get number of results, how can I do that?

This is part of my code:

if (have_posts()) : 

    $args = array(
        'showposts' => '5',
        'paged' => $paged
    );


    $thePosts = query_posts($args);
...

Thank's for help

like image 592
iWizard Avatar asked Jun 18 '12 15:06

iWizard


People also ask

How do I get a total post count in WordPress?

Simply copy the [sbs_posts] shortcode and add it to any WordPress post, page, or shortcode enabled sidebar widget. It will show the total number of published posts on your WordPress site. You can also use [sbs_blog_stats] which will show all blog stats including the total number of posts.

How do I find post queries in WordPress?

Place a call to query_posts() in one of your Template files before The Loop begins. The WP_Query object will generate a new SQL query using your parameters. When you do this, WordPress ignores the other parameters it receives via the URL (such as page number or category).


3 Answers

SOLVED:

if (have_posts()) : 

        $args = array(
            'showposts' => '5',
            'paged' => $paged
        );


        $thePosts = query_posts($args);


         global $wp_query; 
         echo $wp_query->found_posts;
    ...
like image 139
iWizard Avatar answered Sep 29 '22 21:09

iWizard


To display the number of results of a search, use:

Search Result for 

<?php 
/* Search Count */ 
$allsearch = &new WP_Query("s=$s&showposts=-1"); 
$key = wp_specialchars($s, 1);
$count = $allsearch->post_count; _e('');
 _e('<span class="search-terms">'); 
echo $key; _e('</span>'); 
_e(' &mdash; '); 
echo $count . ' ';
 _e('articles');
 wp_reset_query(); 
?>

This was taken from: WP Beginner.

like image 22
Ryan B Avatar answered Sep 29 '22 21:09

Ryan B


The correct answer is

 if (have_posts()) : 

    $args = array(
        'showposts' => '5',
        'paged' => $paged
    );


    $thePosts = query_posts($args);



     echo $thePosts ->found_posts;
...
like image 37
Gabriel Reguly Avatar answered Sep 29 '22 21:09

Gabriel Reguly