Wordpress - Pagination for pages - php

I have a page with the most rated posts.
I use WP-PostRatings and I use this code:
query_posts( array( 'meta_key' => 'ratings_average', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) );
Is there a way to create pagination for pages?
I found something here Wordpress pagination with static pages , but it shows me in all pages, the newest posts, order by date (as in homepage)
Thank you!

To get pagination to work with query_posts() you need to add the $paged variable to your query:
$posts_per_page = 10;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query = array(
'meta_key' => 'ratings_average',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts_per_page' => $posts_per_page,
'paged' => $paged
);
query_posts( $query );

Related

Wordpress category of CPT pagination page 2 ERROR

I got problem of my pagination in category pages >>
I made a category taxonomy for my CPT and when I add posts and posts goes to page 2 .. the page 2 got a ERROR not found
<?php
// ==== Query Dynamic Options ====//
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'portfolio',
'post_status' => 'publish',
'posts_per_page' => 9,
'paged' => $paged,
'order' => 'DESC',
'orderby' => 'ID',
'taxonomies' => 'portfolio-categories',
);
$the_query = new WP_Query( $args );
if you wanna see more code that will helps u to help me ask it for me I will capture it

Wordpress - ajax load more plugin showing tags

I am usign Ajax Load More plugin for showing post. I Want to my posts to show tags, that user insert when he is writting posts. Does any know how can I do that.
I tried to add tags (in ajax-load-more/ajax-load-more.php) in initial query arguments
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => $postType,
'posts_per_page' => $posts_per_page,
'offset' => $offset + ($posts_per_page*$page),
'order' => $order,
'orderby' => $orderby,
'post_status' => $post_status,
'ignore_sticky_posts' => true,
'paged' => $paged,
'tags' => $tags
);
This doesn't work, does any have some example or idea?
Thank you!
If you need filter posts by multiple tags - you should use 'tag__in' => array('tags') instead of 'tags'. Or if single tag - 'tag' => 'slug' or 'tag_id' => 'tag_id'. See WP_Query refference

Paginated Custom Post Type Query with Arguments Array

Im successfully filtering my Porducts by a meta_key and its value. I display (query) the result in a custom page template. The query includes also a Pagination. Everything works fine - BUT sometimes after 3 to 5 page refreshes the query outputs some products twice - especially after calling the next page.
I figured out that the problem is caused by the 'orderby' => 'meta_value_num' part of the following code. Any idea why this happens and any idea how to solve this problem?
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'meta_key' => '_recoed',
'meta_compare' => '>',
'meta_value' => '0',
'post_type' => 'product',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => $paged
);
query_posts($args);
get_template_part('index', 'most-liked-products');
The Part in my Page template looks like
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if (isset($_GET['pnum']) && $_GET['pnum'] > 1) { $paged = 2; } //stop ads from repeating ?>
<?php
get_template_part('index-masonry-inc');
endwhile;
else :
?>
....
<?php endif; ?>
I solved the Problem by using 'orderby' => 'meta_value meta_value_num', instead of 'orderby' => 'meta_value_num', after realizing that the the 'orderby' => ' argument can take more then one parameter! Hope this helps others too. (More about the WP Query)
Side Note: Maybe one of you can tell us why this is more precise than only one parameter. Feel free to comment below. thx
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'meta_key' => '_recoed',
'meta_compare' => '>',
'meta_value' => '0',
'post_type' => 'product',
'orderby' => 'meta_value meta_value_num', // The OrderBy argument can take more then one parameter
'order' => 'DESC',
'paged' => $paged
);
query_posts($args);
get_template_part('index', 'most-liked-products');

Wordpress custom query: 'orderby' => 'date' not working when using multiple post types

In a Wordpress page template, I set up a WP custom query which queries a custom post type named "recipe" AND the regular posts as below. This works, but 'orderby => 'date' in there doesn't work: The page first lists the regular posts ordered by date, then the recipes by date. But I need ALL of them together (i.e. mixed) ordered by date.
Here's the definition of my custom query:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$number_of_posts = get_option('posts_per_page', 12);
$args = array(
'post_type' => array('recipe', 'post'),
'post_status' => 'publish',
'posts_per_page' => 12,
'orderby' => 'date',
'order' => 'ASC',
'paged' => $paged
);
$my_loop = new WP_Query($args);
[...followed by the loop...]
I am grateful for any hints what I can do to achieve the desired ordering.
I found a solution myself:
It works when I add remove_all_filters('posts_orderby'); before the custom query is defined. Obviously this resets any other ordering and allows the 'orderby' => 'date' to function as expected. Complete code:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$number_of_posts = get_option('posts_per_page', 12);
remove_all_filters('posts_orderby'); // ADDED
$args = array(
'post_type' => array('recipe', 'post'),
'post_status' => 'publish',
'posts_per_page' => 12,
'orderby' => 'date',
'order' => 'ASC',
'paged' => $paged
);
$my_loop = new WP_Query($args);
[...]
Q1. what template file are you using?
The query below successfully merges the 2 post types together by date.
<?php
$args = array(
'post_type' => array('my_custom_post_type', 'post'),
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'date',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
// loop your content here
<?php include(locate_template('templates/content.php')); ?>
<?php
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>

Wordpress posts_per_page not working

So, I tried all the "add_filter" things on the whole web to get this right but it doesn't work! It always shows me more than 1 post. What's wrong with my code? Latest WP-Version 4.1.1 and no plugins installed.
Here is the code:
<?php
$sticky = get_option('sticky_posts');
if ( !empty($sticky) ) {
$args = array(
'post__in' => $sticky,
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1
);
$slider_query = new WP_Query( $args );
while ( $slider_query->have_posts() ) {
$slider_query->the_post();
?>
<div>
<!-- here we go -->
</div>
<?php
}
}
?>
Okay, I got it. This parameter is missing. Wordpress ignores "posts_per_page" by default for sticky posts.
$args = array(
'post__in' => $sticky,
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1,
'ignore_sticky_posts' => 1
);
Wordpress doesn't play nice with posts_per_page and wp_query.
To fix it you have to use get_query_var, like so:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
And then in your wp_query() you do:
$args = array(
'post__in' => $sticky,
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1,
'paged' => $paged
);

Categories