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

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.

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".

Custom Query pagination 404 Error

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.

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

How to refresh pagination links after submit new query?

I modified WordPress main loop for blog posts, made it query and loop through different types of posts according to what filter is submitted.
At the beginning of the HTML, there is a form with several buttons as option, it's similar to the SA site menu: Questions Tags Users Badges Unanswered.
When visitor click one of the menu button, a new WP_Query is submitted.
Below the options, is the loop of posts, depends on what filter is submitted.
The php codes is basically a standard wordpress loop with customized functions:
$args= !empty($_POST['filter']) ? $_POST['filter']:null;
get_option_nav();
if( my_loop_have_posts(array('filter'=>$args))):
while my_loop_posts(): the_my_loop_post();
get_template_part('contents');
endwhile;
endif;
Things are working fine except that only the default filter get the correct pagination links. After submit a new filter, when click the pagination links, it goes to the default filter's pages. How to modify wordpress pagination links to get it work for the other filters?
Here's the code that I use for query args:
function get_args($args){
$defaults= array(
'order' => 'DESC',
'orderby' => 'modified',
'max_num_pages' =>5,
'paged' => get_paged (),
'post_status' => 'any',
'post_type' => array ('post', 'docs','topics'),
'posts_per_page' => 5,
)
$args = wp_parse_args ( $args, $defaults );
extract ( $args );
if(!empty($args['filter'])){
switch ($args['filter']){
case 'top_voted':
$args['post_type'] = 'docs';
$args['meta_key'] = '_vote_total';
$args['order'] = 'DESC';
$args['orderby']='meta_value_num';
break;
case 'unanswered_questions':
$args['post_type] = 'topics';
$args['blabla'] = 'blabla';
break;
default:
blabla;
break;
}
return $args;
}
and the code for the paged:
function get_paged() {
global $wp_query;
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var( 'paged' );
} elseif ( ! empty( $wp_query->query['paged'] ) ) {
$paged = $wp_query->query['paged'];
}
if ( ! empty( $paged ) )
return (int) $paged;
return 1;
}
When you make a custom query, you need to explicitly call every parameter of the query. Thus, standard ones disappear, as is the pagination case.
Depending on how you are making this new query you could try:
To concatenate at the beginning $query_string, for example
$query = new WP_Query( $query_string . 'order=DESC' ) As it is explained in the Codex:
If you want to preserve the original query parameter information that
was used to generate the current query, and then add or over-ride some
parameters, you can use the $query_string global variable in the call
to query_posts().
To get the pagination from the default query and use it in your custom query
$paged = get_query_var('paged'); $query = new WP_Query( array( 'paged' => $paged ) );
In your case there is something wrong with your get_paged() function. I have tested it and it does not work, but I have not found the bug. Instead, I tried what I usually use, and it works for me:
function get_paged() {
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { $paged = get_query_var('page');
} else { $paged = 1; }
return $paged;
}
Something seems to fail in you else if statement. Let me know if this solves your issue.

Resources