Wordpress Custom Query Pagination. How do i add pagination to this? - php

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.

Related

Wordpress query with pagination

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>

WordPress pagination not working correctly

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/

Show number of child pages the current parent has

I have a Parent page in Wordpress and am using the below code to display information about each of the child pages the parent has. Perfect.
What I need to do elsewhere on the page is display the count of child pages, for example 'This page has X child pages'.
Can anyone help me to do this please?
<?php
$args = array(
'post_type' => 'property',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) :
?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
//content goes here
<?php endwhile; ?>
<?php endif; wp_reset_query(); ?>
You can use like
$pages = get_pages( array( 'child_of' => $post->ID, 'post_type' => 'property'));
$count = count($pages);

Custom sort of featured products in woocommerce

Can any body please help me to sort the featured products only using different sorting attributes like price,date in woo-commerce.
I have tried the code
function sort_by()
{
$args = array(
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
'posts_per_page' => '-1',
'orderby' => $_POST['sorter'] ,
'order'=>'ASC'
);
$featured_query = new WP_Query( $args );
print_r($featured_query);
if ($featured_query->have_posts()) :
while ($featured_query->have_posts()) :
$featured_query->the_post();
$product = get_product( $featured_query->post->ID );
echo the_title();
endwhile;
endif;
wp_reset_query(); // Remember to reset
die();
}
add_action('wp_ajax_sort_by','sort_by');
add_action('wp_ajax_no-prev_sort_by','sort_by');
The above code works when ajax hit but it's cannot sort the feature products according to date, price, popularity and rating.

Pagination showing same posts from page 1 on all other pages

I've recently had a lot of help creating an upcoming events list (see here Showing upcoming events (including todays event)?), as a result my pagination using WP Pagenavi is broken.
At the moment, when you click on page 2 it just shows the same posts as page one. Although the URL does actually change to page/2 page/3 etc.
I have this in my functions.php file:
function filter_where( $where = '' ) {
$where .= " AND post_date >= '" . date("Y-m-d") . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query(
array(
'post__not_in' => array(4269),
'paged' => get_query_var('paged'),
'post_type' => 'whatson',
'exclude' => '4269',
'post_status' => 'future,publish',
'posts_per_page' => 20,
'order' => 'ASC'
)
);
remove_filter( 'posts_where', 'filter_where' );
My loop is then as follows:
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
// content
<?php endwhile; // end of the loop. ?>
<?php if (function_exists('wp_pagenavi')) { wp_pagenavi( array( 'query' => $query ) ); } ?>
Finally solved this with:
function my_filter_where( $where = '' ) {
global $wp_query;
if (is_array($wp_query->query_vars['post_status'])) {
if (in_array('future',$wp_query->query_vars['post_status'])) {
// posts today into the future
$where .= " AND post_date > '" . date('Y-m-d', strtotime('now')) . "'";
}
}
return $where;
}
add_filter( 'posts_where', 'my_filter_where' );
And:
<?php
$wp_query = array(
'post__not_in' => array(4269),
'paged' => get_query_var('paged'),
'post_type' => 'whatson',
'exclude' => '4269',
'posts_per_page' => 20,
'order' => 'ASC',
'orderby' => 'date',
'post_status' =>array('future','published'));
query_posts($wp_query);
?>
<?php
if ($wp_query->have_posts()) {
while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
Content
<?php endwhile; // end of the loop.
} ?>
<?php if (function_exists('wp_pagenavi')) { wp_pagenavi( array( 'query' => $wp_query ) ); } ?>
Do you want it for specific post or for all of them ? If you want general pagination you can make pagination links without plugins with this piece of code :
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
Just add it to your index.php or archives.php and see the magic happens :)
I am not sure what is going on with the rest of your code, but one thing to try that would be a simple test would be to use wp_reset_query() before your new WP_Query just to make sure that the query variables have not been modified.

Categories