How to get number of results from query_posts? - 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

SOLVED:
if (have_posts()) :
$args = array(
'showposts' => '5',
'paged' => $paged
);
$thePosts = query_posts($args);
global $wp_query;
echo $wp_query->found_posts;
...

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(' — ');
echo $count . ' ';
_e('articles');
wp_reset_query();
?>
This was taken from: WP Beginner.

The correct answer is
if (have_posts()) :
$args = array(
'showposts' => '5',
'paged' => $paged
);
$thePosts = query_posts($args);
echo $thePosts ->found_posts;
...

This will give you the results: Showing results 11-20 of 46, for instance.
$args = array(
'cat'=> $cat,
'posts_per_page' => 10,
'paged' => $paged,
's'=> $s
);
query_posts($args);
$startpost=1;
$startpost=10*($paged - 1)+1;
$endpost = (10*$paged < $wp_query->found_posts ? 10*$paged : $wp_query->found_posts);
?>
<h2 class="displayResult">Showing results <?php echo $startpost; ?> - <?php echo $endpost; ?> of <?php echo $wp_query->found_posts; ?></h2>
If this is not a search page, simply remove the line "'s'=> $s".
If you do need it, make sure you declare the variable as $_GET['s'] above.

Easy. To display number of results for this current page, use
// Showing Page X of Y
print filter_var( absint( $GLOBALS['wp_query']->post_count ), FILTER_SANITIZE_NUMBER_INT );
For the total amount of results, use
print filter_var( absint( $GLOBALS['wp_query']->found_posts ), FILTER_SANITIZE_NUMBER_INT );

display numbers of search results :
<?php global $wp_query;
echo $wp_query->post_count; ?>

query_posts( $args );
global $wp_query;
print_r($wp_query->max_num_pages);
It help me.

Related

pagination in page with custom code in wordpress

How to add pagination for comments in page with custom code in Wordpress?
My code:
<?php
<ol class="comment-list">
<?php
$comments = get_comments(array(
'number' => '8',
'status' => 'approve',
'offset' => $offset
));
foreach( $comments as $comment ){
echo $comment->comment_author . '<br />' . $comment->comment_content;
}
?>
</ol>
<?php paginate_comments_links ( array( 'prev_text' => '« PREV' , 'next_text' => 'NEXT »' ) ); ?>
I searched here for solutions, but did not find any. There are old posts from before 2014. Other codes given here do not work. I have been struggling for several hours.
Please help.
How are you setting the $offset variable? Try something like this, code is not tested but it will give you idea to get comments pagination working.
<?php
$current_page = get_query_var( 'cpage' ) ? get_query_var( 'cpage' ) : 1;
$comments_per_page = 8;
$offset = ( $current_page - 1 ) * $comments_per_page;

Cannot get pagination working in Wordpress Page template, already tried and failed a lot

I'm trying to get pagination working in a page template that I am making. I have already been through multiple threads on this website and I've also been searching through the Wordpress.org documentation (and the comments). Every time I reload my page, there is nothing where the pagination should be.
I've tried both get_posts() and WP_query(). WP_query is the one I believe is needed so that's the one I'll be giving you examples from.
Things I've already tried:
Using $paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1;
Also tried that last one with 'paged' instead of 'page'.
Using this in the arguments: 'posts_per_page' =>6, 'paged' => $paged,
And using this in the arguments 'posts_per_page=6&paged=' . $paged, 'paged' => $paged,
Tried resetting the query of my loops with wp_reset_query();
There are some other things I've tried too but the above are the ones I see recommended most often.
This is my full code in its current state (I've removed some things to help you read it).
I have tried to make the first loop with get_posts() instead and that didn't make a difference. I've also tried positioning commands like the wp_reset_query() in several different places.
The eventual goal is to have several of these loops on the same page, all except the first with pagination under them (I will be using ajax to allow users to cycle through the different content).
<?php /* Template Name: Home */ ?>
<div>
<?php
/*
First Loop - This one will stay the same on every page. Uses the same categories as the second loop.
*/
$categories = '
category-name1,category-name2
';
$query = new WP_Query( array( 'posts_per_page' =>4, 'category_name' => $categories ) );
?>
<div>
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div>
<?php get_template_part( 'entry-boxes-1' ); ?>
</div>
<?php endwhile; wp_reset_query(); endif; ?>
</div>
</div>
<div>
<?php
$paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1;
$query = new WP_Query( array( 'posts_per_page' =>6, 'paged' => $paged, 'category_name' => $categories, 'offset' => '4' ) );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>
<div>
<?php get_template_part( 'entry-boxes-1' ); ?>
</div>
<?php
endwhile; endif;
pagination_bar();
?>
</div>
Try this
<?php /* Template Name: Home */ ?>
<div>
<?php
/*
First Loop - This one will stay the same on every page. Uses the same categories as the second loop.
*/
$categories = array('category-name1' , 'category-name2');
$query = new WP_Query( array( 'posts_per_page' =>4, 'category_name' => $categories ) );
?>
<div>
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div>
<?php get_template_part( 'entry-boxes-1' ); ?>
</div>
<?php endwhile; wp_reset_query(); endif; ?>
</div>
</div>
<div>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = new WP_Query( array( 'posts_per_page' =>6, 'paged' => $paged, 'category_name' => $categories, 'offset' => '4' ) );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>
<div>
<?php get_template_part( 'entry-boxes-1' ); ?>
</div>
<?php endwhile;
$total_pages = $query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => false,
'next_text' => false,
));
}
wp_reset_postdata();
endif;
?>
</div>

Pagination fails with CPT and dynamic tax_query

I have a search form. Choose a taxonomy, and select one or more terms. I want to paginate the results.
Here is the code:
<?php
if(isset($_POST['tax_type'])) {
$tax_type=$_POST['tax_type'];
$terms = $_POST[$tax_type];
$term_list = implode("','", $terms);
$term_list1 = implode(", ", $terms);
$term_list = "'".$term_list."'";
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$giftfinder_args = array(
'post_type' => 'products',
'posts_per_page'=>"1",
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => $tax_type,
'field' => 'slug',
'terms' => explode(',',$term_list),
),
),
);
echo '<div class="results"><span class="eyebrow">search results</span>';
echo '<div class="cat_label">“' . $term_list1 . '”</div></div>';
// the query
$giftfinder_query = new WP_Query( $giftfinder_args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $giftfinder_query;
?>
<?php if ( $giftfinder_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $giftfinder_query->have_posts() ) : $giftfinder_query->the_post();
$thumb = get_the_post_thumbnail(null, 'medium');
?>
<div class="prod"><?php if(empty($thumb)) {echo '<div style="width:265px;height:265px;background:#eee;"></div>';} else {the_post_thumbnail('medium');} ?><span class="truncate"><?php the_title(); ?></span></div>
<?php endwhile; ?>
<div style="clear:both;"></div>
<!-- end of the loop -->
<?php
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'twentyfifteen' ),
'next_text' => __( 'Next page', 'twentyfifteen' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>',
) );
?>
<?php wp_reset_postdata(); ?>
<?php else : get_template_part( 'content', 'none' ); endif; ?>
<?php } ?>
First page is fine, but no results appear on paginated pages.
As you can see, I'm trying to use the pagination fix that turns my query into $wp_query:
// the query
$the_query = new WP_Query( $args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $the_query;
But like I said, no results beyond first page.
I was going to try pre_get_posts but I can't figure out how to code it since every query has unique arguments based on the user's choice of taxonomy and searched terms.
I also tried 'add_args' to the paginate_links() function but could not figure out how to format the resulting get string.
BTW, right now, the query just returns one post to make it easy to check pagination.
Any suggestions appreciated!
Not sure if this is the only issue, but it looks like you have a typo.
You are setting $page:
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
But looking for $paged:
'paged' => $paged,

Display Random Product Link

I need PHP-code for WordPress (Woocommerce) to display random product link.
For example, I have Product1 and want to display on this page (in description of my product):
"See also other products: [Product2 - link] and [Product3 - link]"
Don't know how, I just need php code to insert it in post/pages/products and everywhere I want on my site.
I'm not a coder and I found this code, for example, to display page title with link, but it's not what I need
<?php
echo ''.get_the_title($product_id).'';
?>
But how to get random product, don't know, thanks for help.
Try this :
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'post_type' => 'product' );
$random_products = get_posts( $args );
foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
<?php the_title(); ?>
<?php endforeach;
wp_reset_postdata();
The Perfect solution for outputting a single random product which can be achieved using the following code.
<?php
$post_array=array();
$args = array(
'post_type' => 'product',
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
array_push($post_array,get_the_ID());
endwhile;
$random_key = array_rand($post_array, 1);
echo ''.get_the_title($post_array[$random_key]).'';
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
Have tested the same for you. It worked. Lemme Know if the same worked for you too.

Wordpress loop problem: Multiple loops, index.php and is_paged causing duplicate posts on next page

Hey everyone. I'm not sure if what I'm experiencing is a result of a bug (due to the recent upgrade to 3.1.2) or poor coding. Ever since i upgraded to version 3.1.2 ive been experiencing a problem with two loops on my index page.
Here's what I've got for my index page
<?php
if ( ! is_paged() && is_front_page() ) {
echo '<h6 class="sec1 title">FEATURE</h6>';
$sticky = get_option( 'sticky_posts' );
if ( isset( $sticky[0] ) ) {
$args = array(
'posts_per_page' => 3,
'post__in' => $sticky,
'ignore_sticky_posts' => 1);
// Query
$featured_query = new WP_query( $args );
while ($featured_query->have_posts() ) :
$featured_query->the_post();
$featured[] = $post->ID;
get_template_part( 'content', 'featured' );
endwhile;
} // endif sticky
} // endif is_paged
?>
<?php
$sticky = get_option( 'sticky_posts' );
echo '<h6 class="sec1 title">LATEST ARTICLES</h6>';
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$query_args = array(
'posts_per_page' => 10,
'paged' => $paged,
'post__not_in' => $featured,
'post__not_in' => $sticky
);
query_posts($query_args);
if (have_posts() ) :
while (have_posts() ) :
the_post();
get_template_part( 'content', get_post_format() );
?>
<!--<?php trackback_rdf(); ?>-->
<?php endwhile; else: ?>
<div class="box">
<p>
<?php _e( 'Sorry, no posts matched your criteria.' ); ?>
</p>
</div>
<?php endif; ?>
// Navigation comes over here
Say for example the first loop (sticky posts) - which IS NOT paged, yields 3 posts, and the second loop (all other posts) - which IS paged, yields 10 posts. The problem I'm experiencing is that when i move to the next page, the last 3 posts from the second loop on page 1 get repeated at the the top of page 2.
Note: The first loop is only on page 1, and doesn't get repeated on the second page, which is what i intended.
So this is what i tried, i removed the ( ! is_paged() && is_front_page ) condition along with the entire first loop, and the problem got resolved.
What am i doing wrong?
After your first loop, try adding
wp_reset_postdata();
I'm not sure if your trying to only have the first loop on the first page, but if you are, try something like
$paged = get_query_var('page');
if ($paged < 2) :
// Put whatever you want to only show up on the first page here
endif;
Thanks Chris,
I changed your suggestion (which didnt seem to work)
$paged = get_query_var('page');
if ($paged < 2) :
// Put whatever you want to only show up on the first page here
endif;
to
$paged = get_query_var('paged');
if ($paged < 1 ) {
// code goes here
}
It seems as if the first page isnt considered "paged".. "paged" only applies to pages beyond the first page.
this is the updated code for anyone who's interested. Hat tip to Chris. Thanks again.
$paged = get_query_var('paged');
if ($paged < 1 ) {
echo '<h6 class="sec1 title">FEATURE</h6>';
$sticky = get_option( 'sticky_posts' );
if ( isset( $sticky[0] ) ) {
$args = array(
'posts_per_page' => 3,
'post__in' => $sticky,
'ignore_sticky_posts' => 1);
// Query
$featured_query = new WP_query( $args );
while ($featured_query->have_posts() ) :
$featured_query->the_post();
get_template_part( 'content', 'featured' );
endwhile;
wp_reset_postdata();
} // endif sticky
} // endif $paged
?>
<?php
$sticky = get_option( 'sticky_posts' );
echo '<h6 class="sec1 title">LATEST ARTICLES</h6>';
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$query_args = array(
'posts_per_page' => 10,
'paged' => $paged,
'post__not_in' => $sticky
);
query_posts($query_args);
if (have_posts() ) :
while (have_posts() ) :
the_post();
get_template_part( 'content', get_post_format() );
?>
<!--<?php trackback_rdf(); ?>-->
<?php endwhile; else: ?>
<div class="box">
<p>
<?php _e( 'Sorry, no posts matched your criteria.' ); ?>
</p>
</div>
<?php endif; ?>
an alternative to the previous example was one which i built from the ground up before Chris answered is
<?php if ( isset( $sticky[0] ) && ! is_paged() ) {
echo '<h6 class="sec1 title">FEATURE</h6>';
} ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( is_sticky() ) {
get_template_part( 'content', 'featured' );
} ?>
<?php endwhile; ?>
<?php rewind_posts(); ?>
<?php
echo '<h6 class="sec1 title">LATEST ARTICLES</h6>';
global $sticky;
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 10,
'paged' => $paged,
'post__not_in' => $sticky
);
query_posts( $args );
while ( have_posts() ) :
the_post() ;
?>
<?php get_template_part( 'content', get_post_format() ); ?>
<!--<?php trackback_rdf(); ?>-->
<?php endwhile; ?>
https://wordpress.stackexchange.com/questions/126814/wordpress-static-page-pagination/139594#139594
this post solved my problem for multiple loops with pagination.

Resources