Pagination showing same posts from page 1 on all other pages - php

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.

Related

How do I apply my filter only to one store/url?

I've been struggling applying filters to my woocommerce shop. I have a website with multiple store pages and I've found a filter to only shop products published within the last 30 days. I want this filter to only apply to the 'new releases' shoppage, not to the whole website. Currently it's filtering everything, so even pages created before last month won't appear for example.
How do I make sure this filter is only applied to 1 store? Can I apply an IF statement and check current URL before the filter works or something?
Any help is appreciated.
Filter: (from https://www.sitepoint.com/community/t/show-latest-30-days-products-in-woocommerce/258276)
function baba_recent_products() {
//return 'This is where the recent products should show up if I get the shortcode working. ';
global $woocommerce_loop;
extract( shortcode_atts( array(
'per_page' => '48',
'columns' => '2',
'orderby' => 'date',
'order' => 'desc'
), $atts ) );
$meta_query = WC()->query->get_meta_query();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $per_page,
'orderby' => $orderby,
'order' => $order,
'meta_query' => $meta_query
);
ob_start();
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
add_shortcode( 'baba_recent_products', 'baba_recent_products' );
function filter_where($where = '') {
//posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
function baba_recent_products() {
//return 'This is where the recent products should show up if I get the shortcode working. ';
global $woocommerce_loop;
extract( shortcode_atts( array(
'per_page' => '48',
'columns' => '2',
'orderby' => 'date',
'order' => 'desc'
), $atts ) );
$meta_query = WC()->query->get_meta_query();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $per_page,
'orderby' => $orderby,
'order' => $order,
'meta_query' => $meta_query
);
ob_start();
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
add_shortcode( 'baba_recent_products', 'baba_recent_products' );
function filter_where($where = '') {
global $post;
if($post->ID === /*Page ID as int here*/){
//posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
}
return $where;
}
add_filter('posts_where', 'filter_where');

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>

Display only completed orders on my Woocommerce accounts page

In the account area of my woocommerce shopping site you can click 'orders' and see your order i.e. completed, cancelled, on hold etc etc.
However when you click 'collection' (a page I have made within the account area) I want it to display only 'completed' items.
I have tried to put the following code into the functions.php but with no luck:
(ref - https://businessbloomer.com/woocommerce-display-products-purchased-user/)
add_shortcode( 'my_products', 'bbloomer_user_products_bought' );
function bbloomer_user_products_bought() {
global $product, $woocommerce, $woocommerce_loop;
$columns = 3;
$current_user = wp_get_current_user();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
)
);
$loop = new WP_Query($args);
ob_start();
woocommerce_product_loop_start();
Loop part
while ( $loop->have_posts() ) : $loop->the_post();
$theid = get_the_ID();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $theid ) ) {
wc_get_template_part( 'content', 'product' );
}
endwhile;
woocommerce_product_loop_end();
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
and then adding the shortcode:
[my_products]
when you click the collection link it does not display anything - well generates a div with nothing in it!
Is there a better way of doing this or a alternative?
Replace your $args with bellow code
$args = array(
'post_type' => 'shop_order',
'post_status' => 'wc-completed'
);
Then let me know the result. Thanks

Wordpress Multiple Post Type Loop shows same results on every page

So I am using WordPress with an underscores theme. I used code from twenty fourteen's theme with pagination.
if ( ! function_exists( 'public_notices_paging_nav' ) ) :
function public_notices_paging_nav() {
global $wp_query, $wp_rewrite;
// Don't print empty markup if there's only one page.
if ( $wp_query->max_num_pages < 2 ) {
echo ("<h4 class='one-page-results'>End of Results</h4>");
return;
}
$paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
$pagenum_link = html_entity_decode( get_pagenum_link() );
$query_args = array();
$url_parts = explode( '?', $pagenum_link );
if ( isset( $url_parts[1] ) ) {
wp_parse_str( $url_parts[1], $query_args );
}
$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
$format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%';
// Set up paginated links.
$links = paginate_links( array(
'base' => $pagenum_link,
'format' => $format,
'total' => $wp_query->max_num_pages,
'current' => $paged,
'mid_size' => 1,
'add_args' => array_map( 'urlencode', $query_args ),
'prev_text' => __( '← Previous', 'public_notices' ),
'next_text' => __( 'Next →', 'public_notices' ),
'type' => 'list',
) );
if ( $links ) :
?>
<nav class="navigation paging-navigation" role="navigation">
<h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'public_notices' ); ?></h1>
<?php echo $links; ?>
</nav><!-- .navigation -->
<?php
endif;
}endif;
This worked great when I only had one post type, but now I've made a loop that displays two post types and it works. But on every page its the same group of posts.
<?php
$postLoop = array(
'post_type' => array('public_notice_post', 'post'),
'orderby' => 'date',
'order' => 'DESC'
);
$query = new WP_Query( $postLoop );
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
public_notices_paging_nav();
endif;
wp_reset_query(); ?>
Anything will help. I've tried a lot.
Maybe you got a loop with no "wp_reset_query()" and this may caused some bugs on other loop.. just look all your loop to be sure there noone without reset query.
On which kind of page you coded this loop ? This is a custom template or other ?
Sorry for my english, im french.
To fix the original problem of duplicated posts on every page all I had to do was write my code like this. HOWEVER I am still having an issue of page count, but that will addressed elsewhere.
<?php
$page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$ppp = get_option( 'posts_per_page' );
if ( $page == 1 )
$offset = 6;
else
$offset = 6 + ( $page - 1 ) * $ppp;
?>
<?php
$postLoop = array(
'post_type' => array('public_notice_post', 'post'),
'orderby' => 'date',
'order' => 'DESC',
'paged' => $paged,
'posts_per_page'=> $ppp,
'offset' => $offset
);
$wp_query = new WP_Query( $postLoop );
if ($wp_query->have_posts()) :
while ($wp_query->have_posts()) : the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
public_notices_paging_nav();
endif;
wp_reset_query(); ?>

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

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.

Categories