Wordpress paginate_links() not working - wordpress

Using Wordpress 4.0.1
I have query set up to echo out archive posts to a page. Which works fine, here is the query.
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'post_type' => 'post',
'category__in' => array($category[0]->cat_ID), // gets the category ID higher up in the code.
'posts_per_page' => 5,
'paged' => $paged
);
if ($category[0]->cat_ID == 3) {
$today = date('Ymd');
if (!empty($year) && !empty($monthnum)) {
$args['meta_query'] = array(
array(
'key' => 'event_start',
'compare' => '<',
'value' => $today
)
);
} else {
$args['meta_query'] = array(
array(
'key' => 'event_start',
'compare' => '>',
'value' => $today
)
);
}
$args['order'] = 'ASC';
$args['orderby'] = 'meta_value';
$args['meta_key'] = 'event_start';
}
$the_query = new WP_Query( array_merge($wp_query->query, $args) );
I'm getting out the posts and then at the bottom I want a pagination feature.
Here is my pagination query
$args = array(
'base' => add_query_arg( 'pagenum', '%#%' ),
'add_args' => true,
'format' => '/page/%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_next' => true,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
'end_size' => 3,
'type' => 'list'
);
echo paginate_links($args);
But whats happening is the link at the bottom is showing the url twice like this
http://domain.com/category/recent-seminars/page/2domain.com/category/recent-seminars/page/2/?future
This results in 'Nothing Found' (unsurprising), but even if I delete either the first half or second half, I still get nothing found.
I've tried loads of different combinations in the paginate_links query but nothing is working.
EDIT
I've amended the main query to this now
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'post_type' => 'post',
'category__in' => array($category[0]->cat_ID),
'paged' => $paged
);
$today = date('Ymd');
if ($category[0]->cat_ID == 3) {
$args['meta_key'] = 'event_start';
$args['meta_query'] = array(
array(
'key' => 'event_start',
'compare' => '>=',
'value' => $today
)
);
$args['order'] = 'ASC';
$args['orderby'] = 'meta_value_num';
}
This works for the first page, but as soon as I click one of the next page links, it stops working, however If i take out the $args['meta_query'] array, the paging does work, but the posts are in the wrong order.

Related

Blank pages in pagination

My code for pagination in woocomerce is the following:
global $paged;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'hide_empty' => 1,
'depth' => 1,
'posts_per_page' => 30,
'orderby' => 'name',
'order' => 'ASC',
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'paged' => $paged
);
$loop = new WP_Query( $args );
echo '<div class="woocomerce-paginador">';
echo paginate_links( array(
'format' => '?paged=%#%',
'current' => max( 1, $loop->get( 'paged' ) ),
'total' => $loop->max_num_pages
) );
echo '</div>';
wp_reset_postdata();
I can not figured why but some pages are blank.
For example page one is blank, but page two has products.
Well, i figured out what was the problem.
I have products with no price, and pagination was trying to show no price products but instead it showed blank pages, because of condition i have in my code to only show priced products.
I solved the problem with a meta query.
'meta_query' => array(
array(
'key' => '_price',
'value' => 0,
'compare' => '>',
'type' => 'NUMERIC'
)
),

Woocommerce products only "in stock"

There is the following code, the problem is that it displays all the goods in a row, regardless of status, but I would like to display only those that are "in stock".
function get_products($categories = array(), $product_type = 'featured_product', $paged = 1, $post_per_page = -1, $orderby = '', $order = '') {
global $woocommerce, $wp_query;
$args = array(
'post_type' => 'product',
'posts_per_page' => $post_per_page,
'post_status' => 'publish',
'paged' => $paged,
'orderby' => $orderby,
'order' => $order
); }
I'm not really sure what you're trying to do with your code, as your function and your args seem to be unrelated... but if you're just trying to get the products, try a custom loop:
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
),
),
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock',
'compare' => '=',
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
// Do stuff.
endwhile;
endif;
wp_reset_postdata();
Note that I have not tested this and you clearly have something unique going on with how you're trying to display the posts, so you may need to make a few adjustments.

WordPress pagination returning incorrect permalinks for paged links?

I've got a wp query loop and I have added the pagination_links function to return me the pagination. I have an issue where the link to the first page is returning me the current page instead of the link to the first page?
Can anyone see what is wrong with the below?
$posts_per_page = get_option('posts_per_page');
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'posts_per_page' => $posts_per_page,
'post_type' => 'post',
'paged' => $paged,
);
$articles = new WP_Query( $args );
args = array(
'base' => '%_%',
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $articles->max_num_pages,
'show_all' => false,
'end_size' => 1,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => __('Previous'),
'next_text' => __('Next'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => ''
);
echo paginate_links( $args );

One pagination for multiple queries (or how to group a one query)

I have multiple queries in the same page, like this:
$args_1 = array (
'category_name' => get_query_var( 'category_name' ),
'post__in' => $sticky = get_option( 'sticky_posts' );
'orderby' => 'date',
'order' => 'DESC'
);
$sticky_query = new WP_Query ($args_1);
// loop
$args_2 = array(
'category_name' => get_query_var( 'category_name' ),
'post__not_in' => get_option( 'sticky_posts' ),
'category__not_in' => array(11114),
'orderby' => 'date',
'order' => 'DESC'
);
$query_2 = new WP_Query ($args_2);
// loop
$args_3 = array(
'category_name' => get_query_var( 'category_name' ),
'post__not_in' => get_option( 'sticky_posts' ),
'category__in' => array(11114),
'orderby' => 'date',
'order' => 'DESC'
);
$query_3 = new WP_Query($args_3 );
// loop
I would:
1) limit the total of posts per page to 15
2) (I can't do this, I've searched anywhere but I didn't find a solution) Make this "combined" pagination works . Now, and it's logical, the second page is the same as the first page...
Or the solution can be to make only one query and group the posts and order the groups instead?
Final Solution
you don't need custom pagination if you merge two queries and you don't use "offset", so I've removed
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
And added:
echo paginate_links();
You have to set 'order' to 'post__in' value to keep the order given to the posts in the separate queries.
To avoid memory errors (like '"Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes)".') caused by queries that hold many posts, you have to retrive only post ids in the queries. See here.
Here's the final code:
global $wp_query;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$post_per_page = 35; // How many post per page - setup as you need
$nr_of_posts_to_be_analyzed_to_include_only_current_events = 100; // How many post to analyze to include only current events, ie events that are not in category id 11114
$sticky = get_option( 'sticky_posts' );
// posts in category, limited in number otherwise - if you set '-1' and you have 7.000 posts - you exceed the script time limit execution
$category_posts = get_posts(array('category_name' => get_query_var( 'category_name' ), 'posts_per_page' => $nr_of_posts_to_be_analyzed_to_include_only_current_events,'orderby' => 'date', 'order' => 'DESC'));
$category_posts_ids = array();
foreach( $category_posts as $post ) {
$category_posts_ids[]=$post->ID; // Array with posts ID
}
// var_dump($category_posts_ids);
// expired posts in category, limited in number otherwise - if you set '-1' and you have 7.000 posts - you exceed the script time limit execution
$expired_posts = get_posts(array('category__and' => array(11114, get_query_var('cat')), 'posts_per_page' => $nr_of_posts_to_be_analyzed_to_include_only_current_events, 'orderby' => 'date', 'order' => 'DESC'));
$expired_posts_ids = array();
foreach( $expired_posts as $post ) {
$expired_posts_ids[]=$post->ID; // Array with posts ID
}
// first 100 posts in this category per page - the expired posts that are in 100 first posts in this category gives as result an array of not expired posts
$array_where_to_get_post = array_diff( $category_posts_ids, $sticky, $expired_posts_ids );
// 1/3 only the sticky posts
$stickies_posts_args = array(
'category_name' => get_query_var( 'category_name' ),
'post__in' => get_option( 'sticky_posts' ),
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => -1,
'fields' => 'ids' // important: to avoid memory exhausted error we retrieve postids only
);
// 2/3 only the not expired and not sticky posts
$not_expired_posts_args = array(
'post__in' => $array_where_to_get_post,
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => -1,
'fields' => 'ids', // important: to avoid memory exhausted error we retrieve postids only,
'suppress_filters' => false
);
// 3/3 only the expired and not sticky posts
$expired_posts_args = array(
'category_name' => get_query_var( 'category_name' ),
'post__not_in' => get_option( 'sticky_posts' ), // not sticky posts
'category__in' => array(11114), // expired posts
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => -1,
'fields' => 'ids' // important: to avoid memory exhausted error we retrieve postids only
);
$firstQuery = get_posts($stickies_posts_args);
add_filter('posts_join','join_posts_and_events'); // apply some filter
add_filter('posts_orderby', 'order_posts'); // apply some filter
$secondQuery = get_posts($not_expired_posts_args);
remove_filter('posts_join','join_posts_and_events');
remove_filter('posts_orderby', 'order_posts');
$thirdQuery = get_posts($expired_posts_args);
$mergePosts = array_merge( $firstQuery, $secondQuery, $thirdQuery ); // Merge all queries
$uniquePosts = array_unique($mergePosts); // Create an array with unique posts
// Final query
$args = array(
'post_type' => 'any',
'post__in' => $uniquePosts,
'paged' => $paged,
'orderby' => 'post__in', // order the posts as they are (already ordered)
'order' => 'DESC',
'posts_per_page' => $post_per_page,
'ignore_sticky_posts' => 1 // otherwise it breaks pagination
);
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) { $wp_query->the_post();
// outputs what you want
endwhile;
endif;
echo paginate_links();
Try this one below, I merge two queries (post and page). Working for me - display all posts/page with pagination.
<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$post_per_page = 5; // How many post per page - setup as you need
$firstQuery = new WP_Query('post_type=post'); // First query - you should change args
$secondQuery = new WP_Query('post_type=page'); // Second query - you should change args
$post_ids = array_merge( $firstQuery, $secondQuery ); // Merge all queries
$query = new WP_Query(
array(
'post_type' => array('post', 'page'), // You should change post types as you need
'post__in' => $post_ids,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => $post_per_page
)
);
$totalPosts = $firstQuery->post_count + $secondQuery->post_count; // Count posts
if( $query->have_posts() ):
while( $query->have_posts() ): $query->the_post();
echo the_title()."</br>"; // Content
endwhile;
wp_reset_query();
endif;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $totalPosts / $post_per_page, // Total pages
) );
?>
New Solution - Edit
Just adjust the first and second query
<?php
global $wp_query;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$post_per_page = 5; // How many post per page - setup as you need
// First query
$firstQuery = get_posts(array(
'posts_per_page' => -1,
'category_name' => get_query_var( 'category_name' ),
'post__in' => $sticky = get_option( 'sticky_posts' )
));
// Second query
$secondQuery = get_posts(array(
'posts_per_page' => -1,
'category_name' => get_query_var( 'category_name' ),
'post__not_in' => get_option( 'sticky_posts' ),
));
$mergePosts = array_merge( $firstQuery, $secondQuery ); // Merge all queries
$postIds = array();
foreach( $mergePosts as $post ) {
$postIds[]=$post->ID; // Array with posts ID
}
$uniquePosts = array_unique($postIds); // Create an array with unigue posts
// Final query
$args = array(
'post_type' => 'any',
'post__in' => $uniquePosts,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => $post_per_page,
);
$the_query = new WP_Query($args);
if( $the_query->have_posts() ):
while( $the_query->have_posts() ): $the_query->the_post();
echo the_title()."</br>"; // Content
endwhile;
wp_reset_query();
endif;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
?>
Add below parametter into each query for paginating
'paged' => get_query_var('<'page' or something else you defined>')
EDIT: Hmm, maybe this work!
$query1 = new WP_Query($args1); // Has above paginate param
$query1Ids = get_all_id_in_query1($query1);
$args2 = array(
'post__not_in' => $query1Ids,
// Other param
);
$query2 = new WP_Query($args2); // Have above paginate param
$query2Ids = get_all_id_in_query2($query2);
$args3 = array(
'post__not_in' => array_merge($query1Ids, $query2Ids),
// Other param, also have paginate param
);
$query3 = new WP_Query($args3);
Loop each query and show your posts.

Wordpress:Filter search results by custom fields

I want to filter the search results by custom fields
i have a custom field cp_city and i want the that the users filter their result by city so i added a city drop down beside the searchbox and change the query to alter the results but for some reasons it isn't working.
Here is what i tried
<?php
$city = isset($_GET['city']) ? trim($_GET['city']) : '';
$s = $_GET['s'];
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
query_posts( array('s' => $s, 'scat' => $scat, 'post_type' => 'ads', 'ignore_sticky_posts' => 1, 'meta_key' => 'cp_state', 'meta_value' => $city, 'meta_compare' => 'LIKE', 'paged' => $paged, 'orderby' => 'rand') );
?>
I also tried
function SearchFilter($query) {
if ($query->is_search) {
$query->set('meta_key','cp_state');
$query->set('meta_value','london');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
But both of the tries failed. Can anyone figure out the correct solution.
You should do it like this:
<?php
$city = isset($_GET['city']) ? trim($_GET['city']) : '';
$s = $_GET['s'];
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
query_posts(
array(
's' => $s,
'scat' => $scat,
'post_type' => 'ads',
'ignore_sticky_posts' => 1,
'meta_query' = > array(
'key' => 'cp_city',
'value' => $city,
'compare' => 'LIKE'
),
'paged' => $paged,
'orderby' => 'rand')
);
?>

Resources