Custom Query pagination 404 Error - wordpress

I am struggling with the pagination of a custom loop in WordPress …
The Links are displayer correctly, but when I click on the next page I get a 404 Error.
The Link seems to be correct http://…/page/2/
Did I miss something while creating the query?
Do I have to add an additional function in the functions.php file?
This is my query:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wu_media_query = new WP_Query( );
$wu_media_query->query('showposts=5&post_type=wu_media&paged=' . $paged);
Would be grateful any hint :-)
Dominik

First, go to Settings >> Permalinks and hit Save Changes button. Sometime you need to regenerate you .htaccess file to make the pagination work.
Make sure you've called wp_reset_query() after ending the while loop or after the default WordPress loop. Sometimes it make this kinda trouble.
Pass 'paged' => $paged with the query arguments array to the WP_Query
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query_args = array(
'post_type' => 'post',
'category_name' => 'tutorials',
'posts_per_page' => 5,
'paged' => $paged // Like this.
);
Remember, for custom pages that are NOT static home pages, the $paged variable changes to this:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
Hope the above helps you.

Related

Display Category from WP Custom Post

Wrote this code to display specific category from custom post type on wp page.
<?php
$paged = (get_query_var('paged')) ?get_query_var('paged') : ((get_query_var('page')) ? get_query_var('page') : 1);
$blog_items_num = ($data['blog_item_number']) ? $data['blog_item_number'] : 3;
$blog_order = ($data['blog_order']) ? $data['blog_order'] : "date";
$args = array(
'post_type' => 'aeolus_news',
'cat'=>'24',
'posts_per_page'=>$blog_items_num,
'orderby'=>$blog_order,
'paged' => $paged
);
query_posts($args);
rewind_posts();
get_template_part( 'content-news', 'single' );
wp_reset_query();
?>
But nothing displays.
If comment out 'cat' => '24' then all posts/categories from the custom post type display.
Suggestions?
First, consider using WP_Query() instead of query_posts for post queries like this (see this WPSE answer for details why)
Second of all, if you're using the category id, it's expecting an integer not a string, and the apostrophes around the id are making it a string.
Replace 'cat' => '24', with 'cat' => 24, (with no quotation marks around the 24). Right now it's looking for a category literally named "24".

How to query posts only with "<img" in its content? Wordpress

I was working on my personal blog theme, and I want to list all my "image post" (have image(s) in it) to a page, so I built a page template, but don't know how to make WordPress query only those posts with image(s).
I know how to query posts with featured image but my blog is too old and lots of posts don't have a featured image. So if I can search and query post with "
Is there a way to make a custom query to get all posts with
And I find the solution now, a simple search for "img" tag will perfectly do the job, the query goes like this
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query_args = array(
'post_type' => 'post',
's' => '<img',
'posts_per_page' => 9,
'paged' => $paged
);
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop
?>

WordPress index archives not working (Prev / Next)

I'm having a strange issue whereby my blog index archives pages (Previous Link) isn't working and is going to a 404 page.
I have a page set as the site's Index, and the News (Blog) section is located at a seperate page called 'news'. Archive pagination works fine for category archives, it just appears to be the index.php loop that is not allowing the archives pagination to work for some reason. Any help would be greatly appreciated, here's my code from my theme's index.php file that dips into the loop for posts:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
// 'cat' => -13, // Exclude Guide To's
'order' => 'DESC',
'paged' => $paged,
'ignore_sticky_posts' => true
);
$latest_posts = new WP_Query( $args );
if ( $latest_posts->have_posts() ) {
while ( $latest_posts->have_posts() ) {
$latest_posts->the_post(); ?>
<?php get_template_part( 'content', 'post' ); ?>
<? } }
?>
If I understand you correctly, you have set a static front page. In that case
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
is wrong. paged is used for any page, except a static front page. Static front pages uses page. So you should change the above mentioned line to
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
EDIT
Your next_posts_link should also be adjusted for custom queries. You have to set the $max_pages parameter
next_posts_link( 'Older Entries', $latest_posts->max_num_pages );
EDIT 2
Seems like your rewrite rules seems to be the problem. Add the following to your functions.php and reload your index page a few times.
function bt_flush_rewrite_rules() {
flush_rewrite_rules();
}
PLEASE NOTE You should delete this function immediately after reloading your index page a few times. flush_rewrite_rules() are a really expensive operation that is quite tough on resources, so it should ONLY be used if really really necessary

Page url showing page/2 but getting posts from first page

I'm struggling a theme that I got, the theme uses query_posts() and pagination used to work. Now pagination is not working on this page, and it keeps on showing posts from the first page on the second. Meaning, the url shows page/2 but I keep on seeing the first posts of the category, i.e. the first 4 on every page.
Here is the code used to get the posts:
global $current_category;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
global $query_string;
$args = "";
if ($current_category['post_type'] == "product") {
$args=array(
'showposts' => 4,
'paged' => $paged,
'post_type' => 'product'
);
} else {
$args=array(
'showposts' => 4,
'category_name' => $current_category['name'],
'paged' => $paged
);
}
query_posts($args);
And here's the loop:
if (have_posts()) : while (have_posts()) : the_post();
<outputs code here>
endwhile;
else :
<output no results code here>
endif;
if ( is_home() ) wp_reset_query();
Now, can anybody please point me in the right direction?
//update:
I have already tried this solution as well, so far, I can only tell that the paged variable never gets updated in the query.
//update 2:
This page was done custom, and setting it as the home page via Settings -> Reading inhibits the above behaviour. When leaving it as a normal page, and setting home as recent posts, the pagination works fine.
if you have any custom queries in the template, make sure to integrate the 'paged' parameter.
Pagination Parameters
Go through these codex pages:
Preserving Existing Query Parameters
Related: Pagination
I have fixed this after some very long research. My code now looks as follows:
global $current_category, $paged;
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
This just replaces the first 3 lines of the original code in the answer. The rest looks exactly the same.

$paged do not work in wordpress frontpage

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 10,
'paged' => $paged,
);
query_posts($args);?>
I used this code in WP page template, then published a page with this template, then set this page as static homepage, then I find paged always equals 1.
when I click page 2, I still get same content as page 1, although url is correct ,such as /page/2/
i find the answer myself.
just use
get_query_var('page')
instead,it will work.

Resources