Display Wordpress posts in Chronological (Ascending) Order - php

By default Wordpress shows all posts in reverse chronological order (with the newest post first).
I would like to display all of my wordpress posts in chronological order (with the oldest posts shown first).
I am trying to use a custom loop query to do this, however I cannot get it to work. What am I missing here?
<?php query_posts(array('orderby'=>'date','order'=>'ASC'));
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<div class="postTitle"><?php the_title(); ?></div>
<div class="postContent"><?php the_content(); ?></div>
<?php endwhile; endif;
wp_reset_query();
?>
I thought this would be quite simple, although everything I have found to try I also cannot make work. thanks!

Using custom loop:
If you are creating a custom loop you might want to use WP_Query instead.
<?php
$the_query = new WP_Query([
'order'=>'ASC'
]);
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) :
?>
<div class="postTitle"><?php the_title(); ?></div>
<div class="postContent"><?php the_content(); ?></div>
<?php
endwhile;
/* Restore original Post Data */
wp_reset_postdata();
?>
<?php else: ?>
// no posts found
<?php endif; ?>
Using filters
Or another method is to alter the main loop using filters in your functions.php file.
function alter_order_of_posts( $query ) {
if ( $query->is_main_query() ) {
$query->set( 'order', 'ASC' );
}
}
add_action( 'pre_get_posts', 'alter_order_of_posts' );
I suggest the filter path to avoid changing a lot of your current template.

Related

Should I place wp_reset_postdata(); after endwhile; or endif; ?

I have spent some time reading through the WordPress Codex, as well as various themes, and can see that some developers insert <?php wp_reset_postdata(); ?> after the endif; in a Blog Loop whilst others insert the code between the endwhile; and endif; of a Blog Loop. I have tried both locations but have yet to see a difference. Is there a correct location?
This function supposed to reset the secondery query that you run.. the function the_postis letting you to use all the functions that you can run in the loop like the_title() the_content and so on..
So you reset the the_post function and after the endwhile; you can reset it already. and use your main query inside the if statment too if you like.
<?php
// this is the main query check if there is posts
if ( have_posts() ) :
// loop the main query post and run the_post() function on each post that you can use the function the_title() and so on..
while ( have_posts() ) : the_post();
the_title(); // title of the main query
// the is second query
$args = array( 'posts_per_page' => 3 );
$the_query = new WP_Query( $args );
// check if there is a posts in the second query
if ( $the_query->have_posts() ) :
// run the_post on the second query now you can use the functions..
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
the_excerpt();
endwhile;
// reset the second query
wp_reset_postdata();
/* here we can use the main query function already if we want..
its in case that you want to do something only if the second query have posts.
You can run here third query too and so on...
*/
the_title(); // title of the main query for example
endif;
endwhile;
endif;
?>
wp_reset_postdata() is only required if you have a secondary loop (you're running additional queries on a page). The purpose of the function is to restore the global post variable back to the current post in the main query.
Example:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
OUTPUT MAIN POSTS HERE
<?php endwhile; endif; ?>
In the code above I'm simply using the main loop to display posts. There's no reason to include wp_reset_postdata(). The global post variable is exactly what it's supposed to be.
If somewhere else on the page I decide to add a secondary loop, then I'll need wp_reset_postdata().
// This query is just an example. It works but isn't particularly useful.
$secondary_query = new WP_Query( array(
'post_type' => 'page'
) );
if ( $secondary_query->have_posts() ) :
// Separating if and while to make answer clearer.
while ( $secondary_query->have_posts() ) : $secondary_query->the_post();
// OUTPUT MORE STUFF HERE.
endwhile;
wp_reset_postdata();
endif;
To answer your original question: it usually comes after endwhile and before endif. It's the call to the_post() which actually changes the global post variable. If there are no posts then the post variable remain the same and there's no reason to use the reset function.

Wordpress Trying to use get_title() in loop array as the category name

On my wordpress page template Im trying to use the get_title() in my arguments array for the a loop but i cant get it to return any posts.
This is where im up to;
<?php
$args = array(
'category_name' => 'the_title();'
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="entry"><?php the_content(); ?></div>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
Im unable to get any posts to return on this page unless i manually enter the slug for the category i want to use which is not ideal as the page slug will always be the same as the post category slug.
My logic behind this is that i dont want to have an individual page templeate for each page with the different cat as i will end up with hundreds of page templetes.
Any help appreciated
Cheers
Jon
You're passing the_title(); as a string do to ' '. Also, the_title() gets title of the object in the loop, not of a category.
You can do the following:
global $post;
$args = array('category_name' => $post->post_name);
You have passed the_title(); as a string, so will be treated as string, not a function, so it will not return title. You can use global variable $cat, So your code will be,
<?php
global $cat;
$args = array('cat' => $cat);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="entry"><?php the_content(); ?></div>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>

WordPress Category Pagination not working

I have two custom post types from the archive.php One is archive-slug.php and the other is category-slug.php. The pagination works on the archive-slug.php but the same code on the category-slug.php won't even show. I'm somewhat new to Wordpress and php so i'm sure i'm missing something here, I just don't know what?
<?php
// Custom Post Type
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'available_dogs',
'category_name' => 'adopted-dogs',
'posts_per_page'=> 9,
'paged'=> $paged
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="dog-info-box col-lg-4 col-sm-6 col-xs-12">...</div>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
<?php
// Get the pagination
fusion_pagination( $pages = '', $range = 2 );
?>
<?php if( $sidebar_exists == true ): ?>
<?php wp_reset_query(); ?>
As I want to inform you that when you create a file "category-slug.php : it's an default template.No need to add the query posts.
It will display the posts of the category whose slug is added in file like category-slug.php.Here is the modifying code.
if (have_posts() ) :while ( have_posts() ) : the_post();
the_content();//content is going on.
endwhile;//pagination functionfusion_pagination( $pages = '', $range = 2 );else : _e( 'Sorry, no posts matched your criteria.' ); endif;
Precaution :1:Pagination code should be placed just before of the while loop.2:In your code pagination code have been place after the endif that's why it is not working.
Thanking you.
I used this code and got it working. It's not pretty but it will do for now. For what ever reason when I try to use it after the loop it doesn't work. When I use it after the endif it works.
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
</div>
</nav>
<?php } ?>
Please follow this steps.
1: Please make sure, your category have the posts.
2: If your category slug is "test" then your file name should be " category-test.php . if any confusion then please let me know.
3: See the code and write in file.
if(have_posts()) :while(have_posts()): the_post();the_title();the_content();endwhile;//pagination of wp
previous_posts_link( '« Newer Entries' );
next_posts_link( 'Older Entries »', 0 );endif;
Note:1:Please place this code in file and run if it will running successfully, then we will add the designing part.
2: If you can share the website URL then I can also give suitable solution because then I can seen the category slug etc .
Please let me know if still needs problem.
Thanking.
I finally fixed a variation of this same issue.
This issue was occurring with category archive pagination.
Initially thought it was within the Loop - turns out the theme I was working on had these filters in a file, custom.php, which manually rewrote the permalink structure.
add_filter('category_link', 'themename_change_archive_link', 100, 2);
add_action('init', 'themename_archive_rewrite', 50);
Pagination didn't like that due to what looks like a conflict with a recent WooCommerce update, so I removed these filters, used the Custom Permalink plugin to rewrite the category permalinks, and it worked! Now I have a custom permalink structure AND functioning category pagination.

WP Multiple Loops - Most Recent Post & Pagination

I'm hoping someone can help me figure this one. It's been giving me problems for a bit. I'm attempting to have a custom category page display the most recent post at the beginning of the page, and below in a separate loop I wish to display the rest of the posts in the given category WITH pagination, offsetting the loop by 1. I've searched numerous ways to do this and can't quite come up with a solution! Any help you can give for this one I'd be soooo grateful for! Thank you in advance! Here's my code:
<?php query_posts( 'cat=3&showposts=1&offset=0'); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="firstPost">
Snipped post style
</div>
<div class="prevHeader">Previous Episodes</div>
<?php endwhile; ?>
<div class="container archiveContainer">
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( 'cat=3&posts_per_page=6&offset=1&paged=' . $paged );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="prevEntries">
Snipped Post Style
</div>
<?php endwhile; ?>
</div>
<div style="width:100%">
<span style="display:inline-block; margin-left:10px; float:left" class="nav-previous"><?php next_posts_link( '<h4>Older posts</h4>', $the_query->max_num_pages ); ?></span>
<span style="display:inline-block; margin-left:10px; float:right" class="nav-next alignright"><?php previous_posts_link( '<h4>Newer posts</h4>' ); ?></span></div>
<?php
// clean up after the query and pagination
wp_reset_postdata();
?>
<?php endif; ?>
This can certainly be done, but surely not with query_posts. It a case like this, it will outright fail. Also, offsets breaks pagination and are quite difficult to work with if you don't know what you are doing
I don't have time to code now, but here is an idea:
If you need to display the latest post on all your pages, then you can do the following.
Remove you custom query and your offset so that everything works normal. Make use of the default loop and paginate as normal
To display your first post on every page, create a custom query with WP_Query and place it where you need to display this post .See the link on how to properly construct that query. Please do not use query_posts
You will now see that you have the first post displayed twice. To counter this, wrap your custom query in a is_paged() condition.
EDIT
You can try something like this. (PS! showposts is depreciated, use posts_per_page. Also, you can removed the paged parameter when setting an offset as it will be ignored)
(CAVEAT Untested)
if( is_paged() ) {
$args = array(
'posts_per_page' => 1,
'cat' => 3,
);
$q = new WP_Query( $args );
if( $q->have_posts() ) {
while( $q->have_posts() ) {
$q->the_post();
// Display your loop elements
} //end while
wp_reset_postdata();
} // end if have_posts()
} //end if is_paged
// Now for your main loop
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$the_query = new WP_Query( 'cat=3&posts_per_page=6&paged=' . $paged );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
// Your loop elements
}
wp_reset_postdata();
}
Codex states : Specifying hard-coded offsets in queries can and will break pagination since offset is used by WordPress internally to calculate and handle pagination.
See the following link. As described on the page you'll need to use a hook and filter.

How do I get wordpress to override a previous posts query?

I've got a page that has a category list at the top, and should normally list posts below it. The category list is created using:
<?php $display_categories = array( 4, 7, 8, 9, 21, 1); $i = 1;
foreach ( $display_categories as $category ) { ?>
<div>
<?php single_cat_title(); ?> //etc
</div>
<?php }
?>
However, this seems to make the post loop order posts by category. I want it to ignore category ordering and order by date in descending order. I've created a new WP_Query since according to the docs you can't use query_posts() twice, so just in case.
<?php $q = new WP_Query( "cat=-1&showposts=15&orderby=date&order=DESC" );
if ( $q->have_posts() ) :
while ( $q->have_posts() ) : $q->the_post(); ?>
the_title(); // etc
endwhile;
endif;
?>
However, this still seems to be ordered by category (the same order as the list above) and then by date, as opposed to just by date.
I've had problems with this before as well.
Try this:
<?php
global $post;
$myposts = get_posts( 'numberposts=5' );
foreach( $myposts as $post ) : setup_postdata( $post ); ?>
<div <?php post_class(); ?>>
<div class="title">
<h2>
<?php the_title(); ?>
</h2>
<p class="small"><?php the_time( 'F j, Y' ); ?> by <?php the_author(); ?></p>
</div>
<?php the_excerpt(); ?>
</div>
<?php endforeach;
?>
The important line is global $post;.
That should reset your global query. The setup_postdata($post) method is necessary to give you access to functions like the_author() or the_content().
-Chris
I don't have any experience with wordpress, but a couple of possibilities:
You define the "order" parameter twice in the string you're calling query_posts() with, I don't know if that causes a problem or not.
As well, "show" is not a valid parameter, you may have been looking for "showposts".
Parameters and their effects are described here: http://codex.wordpress.org/Template_Tags/query_posts#Parameters
query_posts is finicky sometimes. Try something like this and see if it works:
query_posts(array('category__not_in'=>array(1),
'showposts'=>15,
'orderby'=>date,
'order'=>DESC));
Since that's not the issue, try adding update_post_caches($posts) to the second loop, like this:
<?php $q = new WP_Query("cat=-1&showposts=15&orderby=date&order=DESC");
if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post(); update_post_caches($posts); ?>
the_title(); // etc
endwhile; endif; ?>
Supposedly this solves some plugin problems.

Categories