WordPress pagination not working after first wp_query. I am performing some operations on the data returned by first wp_query object. but after the second query, pagination is not working.
<?php
$prop_no = intval( get_option('wp_estate_prop_no', '') );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'estate_property',
'author' => $current_user->ID,
'paged' => $paged,
'posts_per_page' => $prop_no,
'post_status' => array( 'any' ),
);
$prop_selection = new WP_Query($args);
//some code
$args = array(
'post_type' => 'estate_property',
'author' => $current_user->ID,
'paged' => $paged,
'posts_per_page' => $prop_no,
'post__in' => $sorted_posts,
'orderby' => 'post__in'
);
$prop_selection = new WP_Query($args);
?>
I tried removing the 'paged' parameter from one of the queries, that gives either wrong results or no pagination. ex. this query returns 28 results first time but only returns 6 results after second query. And if I remove the 'paged' parameter from first query and add it to second query then only one page is returned when it should return 3 pages.
Create Function in function.php
function sofg_pagination($max_num_pages,$paged,$page_id){
if($max_num_pages > 1){
echo '<div class="post-wrap pgns">';
echo '<ul class="pagination_list">';
echo '<li>First</li>';
for($i=1; $i<= $max_num_pages; $i++){
if($paged==$i){
echo '<li class="active">'.$i.'</li>';
}
else{ echo '<li>'.$i.'</li>'; }
}
echo '<li>Last</li>';
echo '</ul></div>';
}
}
Get Pagination Your listing page
Define Before loop
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$page_id = get_the_ID();
Use After Loop
$max_num_pages_1 = $max_num_pages->max_num_pages;
sofg_pagination($max_num_pages_1,$paged,$page_id);
See pagination http://hiddenwhy.igexsolutions.com/blog/
Related
Currently I am trying to get wordpress pagination to display at the bottom of the page but nothing shows up, if I echo out "paged" it outputs 1 so I believe it is correctly reading the page it is on. The loop I am using is as follows:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'company_list', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => 6, 'paged' => $paged);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
To echo out the actual pagination at the bottom of the page I am using:
echo paginate_links( $loop );
Currently no pagination is being displayed at the bottom of the page but the loop is working correctly. The page is also not set as a static front page.
Thanks.
1)You do not have to use question mark in your first row
$paged = (get_query_var('paged')) ?
2)also see :
https://stackoverflow.com/a/33757648/10210277
I need a bit help from you. I have a custom search engine to search products from a post type taxonomy :
if( isset($_POST['search_products'] ) {
/// codes ....
$_SESSION['ids'] = $my_ids;
$args = array(
'post_type' => 'product',
'showposts' => -1,
'post__in' => $_SESSION['ids']
)
$posts = new Wp_Query($args);
}
This query outputs about 60 products with pagination (10 products per page), but when user visits the page without using the search engine, all products should be displayed. Instead, the $_SESSION remains and display only the previous results.
I just want the pagination working when I do search, and all products displayed when I access the page without using the search engine.
Does any Wordpress expert have an idea ?
Thank you.
if( isset($_POST['search_products'] ) {
/// codes ....
$_SESSION['ids'] = $my_ids;
$args = array(
'post_type' => 'product',
'showposts' => -1,
'post__in' => $_SESSION['ids']
)
$posts = new Wp_Query($args);
}
else {
$args = array(
'post_type' => 'product',
'showposts' => -1,
)
$posts = new Wp_Query($args);
}
simply put an else block
CODE ,
global $post;
$id = intval($_GET['cat']);
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$products = new WP_Query( array(
'post_type' => 'products',
'order' => 'ASC',
'posts_per_page' => 5,
'paged' => $paged
) );
endwhile;
HTML ,
<ul class="pagination pull-right">
<li><?php echo get_next_posts_link( 'Next Page', $products->max_num_pages ); ?></li>
<li><?php echo get_previous_posts_link( 'Previous Page' ); ?></li>
</ul>
I use WP Beginner pagination (without plugin) on my website, but I can't access to second and other pages.
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$custom_query_args = array(
'post_type' => array( 'tutorials','post' ),
'posts_per_page' => 2,
'cat' => $cat_id,
);
// Get current page and append to custom query parameters array
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// Instantiate custom query
$custom_query = new WP_Query( $custom_query_args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
// Output custom query loop
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
echo '<article class="other-post col-xs-12 col-sm-12 col-md-3 col-lg-3">';
echo '<div class="back-color">';
echo '<h3>';
echo the_post_thumbnail('post-thumbnail', array( 'class' => 'post-image' ));
echo '<a href="'. get_permalink() .'" title="'. get_the_title() .'">';
echo '<span><b>'. get_the_title() .'</b></span>';
echo '</a>';
echo '</h3>';
echo '</div>';
echo '</article>';
endwhile;
endif;
// Reset postdata
wp_reset_postdata();
// Custom query loop pagination
wpbeginner_numeric_posts_nav();
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
Here is code from function.php, but I think it isn't the problem.
http://virtual-wizard.eu/function.txt
You’re missing the paged parameter in your query args.
I would also replace the page query, so instead of this:
// Get current page and append to custom query parameters array
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
You have this:
// Get current page and append to custom query parameters array
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var( 'paged' );
} elseif ( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
} else {
$paged = 1;
}
And then change your query to this:
$custom_query_args = array(
'post_type' => array( 'tutorials','post' ),
'posts_per_page' => 2,
'cat' => $cat_id,
'paged' => $paged,
);
I hope that helps.
Try using this code instead. Make sure to read through as they are explaining how to implement this function into your WP website.
http://www.kriesi.at/archives/how-to-build-a-wordpress-post-pagination-without-plugin
Function is setup inside of a functions.php and on the specific page when you want to display pagination just call the function like pagination();
Before calling out pagination check also the CSS part and structure of the pagination to get the desired effect.
Srecno!
I am currently getting all the post from the db via a query. The php code included is part of a custom Wordpress template. I am using custom meta boxes from: https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress
what do i need to do to paginate every 6 post?
<?php
//get_template_part( 'content', 'page' );
echo'<h2 class="section-header-dark">'.get_the_title().'</h2><hr>';
//adjusting the query
$args = array(
'post_type' => 'blog',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => ASC
);
// The Query
$latest_post = new WP_Query( $args );
// The Loop
if ( $latest_post->have_posts() )
{
while ( $latest_post->have_posts() )
{
$latest_post->the_post();
$desc = wpautop( get_post_meta( get_the_ID(), '_cw_desc', true ) );
echo'<div class=""><h5 class="">'. get_the_title(). '</h5>';
echo $desc;
echo'<h6 class="news_date f_right"><i>'. get_the_date(). '</i></h6>';
echo'</div><hr>';
}
}
else
{
// no posts do nothing
}
wp_reset_postdata();
?>
You need to set 'posts_per_page' => 6 and add the paged Paramter to the query.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 6,
'paged' => $paged
);
?>
Visit http://codex.wordpress.org/Pagination for more details.
I'm having difficulty pulling only the desired category into an archive page on Wordpress. My code is below, I thought by defining get-category-by-slug this would work, but instead its pulling all of the post from every category into the page.
<?php
$category = get_category_by_slug('weddings');
$args = array(
'post_type' => 'elp_projects',
'posts_per_page' => 12,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
query_posts($args);
$x = 0;
while (have_posts()) : the_post();
?>
Any ideas on how to fix this would be appreciated.
I've also tried these combinations with no luck.
<?php
$category = get_category_by_slug('weddings');
$args = array(
'post_type' => 'elp_projects',
'posts_per_page' => 12,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
$query = new WP_Query( 'category_name=weddings' );
$x = 0;
while (have_posts()) : the_post();
?>
and
<?php $query = new WP_Query( 'category_name=weddings' ); ?>
and
<?php
$args = array(
'post_type' => 'elp_projects',
'posts_per_page' => 12,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
$query = new WP_Query( 'category_name=weddings' );
$x = 0;
while (have_posts()) : the_post();
?>
Please do not use query_posts its not the best way to query data its a last resort if you will.
Instead use WP_Query to get posts from one category just do $query = new WP_Query( 'category_name=staff' ); refer to this page for more information on how to get posts from one category using WP_Query.
EDITED
Try this
$the_query = new WP_Query( array(
'post_type' => 'page',
'orderby' => 'date',
'category_name' => 'wedding', //name of category by slug
'order' => 'DESC',
'posts_per_page' => )); // how many posts to show
// Put into the loop
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
// Restore original Post Data if needed
wp_reset_postdata();