WordPress index archives not working (Prev / Next) - wordpress

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

Related

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
?>

sectioning content within a wordpress page

Building a wordpress site that needs to organize content by stream - e.g Mechanical, Electrical etc. Also, each page needs sections like News, Articles events etc. If you pick one page (say Mechanical) it has to have the following sections
News
Articles (category:articles)
Events (category:events)
The other streams will have the same sections as well
Is there a plugin for achieving or would I be better off building a template page for each vertical and writing php code? Shown code for a page with a single section.
<?php
$args = array(
'posts_per_page' => 1,
'category_name' => 'news',
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args );
$the_query = new WP_Query($args);
//EXAMPLE NEWS SECTION
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_content();
echo $content;
}
} else {
// no posts found
}
?>
In my opinion you could just write a plugin which does that filtering.
Said plugin would have some kind on shortcode that would take a parameter (category for instance) and would return or echo all the posts associated with that category.
Shortcode registration :
add_action('init','register_shortcode');
function register_shortcode(){
add_shortcode('shortcode_name', 'shortcode_function');
}
Shortcode function:
function shortcode_function($attr){
$attr = shortcode_atts(array('category' => 'default'),$attr); //Setting defaults parameters
$args = array(
'category_name' => $attr['category'],
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//echo other html related stuff
the_title();
the_content();
}
}
}
Usage
[shortcode_name category='news']
[shortcode_name] //category defaults at 'default'
In your case a page could be something like that
<div>
News : <br/>
[shortcode_name category='news']
</div>
<div>
Articles : <br/>
[shortcode_name category='articles']
</div>
<div>
Events : <br/>
[shortcode_name category='events']
</div>
I don't think there is one right way to do this... it depends on the scenario... How many posts, how often do sections or "main streams" get added / change, do you need other stuff on the pages etc.
One pretty simple and easily maintained solution would be to:
1. Add mechanical, electrical, etc. as categories alongside news, articles and events
2. Modify (or add) category.php to repeat the loop three times, and only display the posts that belong to each section (other category):
//Loop your sections
foreach(array('news', 'articles', 'events') as $category) {
echo '<h2>' . $category . '</h2>';
//Loop posts
while ( have_posts() ) {
the_post();
//Check if post is in section (by slug!)
if ( in_category($category) ) {
the_title();
the_content();
}
}
//Start the loop over
rewind_posts();
}
Now just assign each post to one (or more) "parents", fx mechanical, and also to one (and only one) of news, articles or events...
If you go to the archive page of mechanical you get three sections
if you go to news you get all news (but also two empty sections, so you should of course check for that).
Note that this could be done using tags or even a custom taxonomy if you wanted, you'd just need to edit a different theme file - see the template hierarchy.
Beware though that this will not work very nicely with pagination, so if that is a concern you will need to deal with that.

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.

enabling pagination inside wordpress post

i have a specific single page which display a single post.
the thing is that i want to display below it all the other posts which have the same special meta data and i made that to work as well.
the problem starts when i try to make pagination to the list of the posts below.
the single post url is something like that:
blog.com/somepost
and the pagination link to the second page of posts below looks someting like this
blog.com/somepost/page/2
and wordpress automatically redirects me back to
blog.com/somepost
how can i prevent it from redirecting me back?
btw, i"m using something like that:
i"m doing something like this:
while( have_posts() ): the_post();
//here printing the single post
endwhile;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'meta_key' => '_btree_project_id',
'meta_value' => $post->ID,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 8
);
$temp = $wp_query;
$wp_query = new WP_Query( $args );
while( have_posts() ): the_post();
//looping through the related posts here
endwhile;
The reading I've done about WordPress pagination gives me the impression that it is an imperfect feature. It requires the global var $wp_query which stems from the WP_Query object. WP_Query holds the global $wp_query which is necessary for making even basic pagination work. Custom queryies don't have access to $wp_query, nor do they own a var to control pagination. I assume you are using a custom query to grab that single post, and as this article points out, with custom queryies:
the “fix” is to trick WordPress into
using the global $wp_query variable
when using our own custom loops.
The article gives an example of utiilizing the global var in your custom query so that you have access to the query_vars that make pagination possible.
I expect that your permalink structure and a custom query that I'm guessing you are using might not be working because the global $wp_query var isn't available during your loop to show related posts.
What does your code to get, display, and paginate the related posts look like? Can you post?

Resources