Sort by expire date
-
Hi. Is there any way to sort the product list of dashboard by expire date? Plugin´s function is ok but i have many products and have to check one by one to know wich are going to expire soon and contact customers to renew contracts.
-
I use this in one of my templates to sort by expiration:
<?php $the_query = new WP_Query(array( 'orderby' => 'meta_value', 'meta_key' => '_expiration-date', 'posts_per_page' => -10, 'order' => 'ASC' )); ?> <?php if ( $the_query->have_posts() ) : ?> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <!-- Do Something --> <?php endwhile; wp_reset_postdata(); ?> <?php else : ?> <!-- Do Something --> <?php endif; ?>You may be able to use the is_admin() in a function to work in the admin.
Thank you. But where i should paste that code?
I was able to get the following to work. Try inserting the code in your theme’s functions.php file:
/* Sort posts by expiration */ function sort_query_order( $query ) { if ( is_admin() && $query->is_main_query() ) { $query->set( 'meta_key','_expiration-date' ); $query->set( 'orderby','meta_value_num' ); $query->set( 'order', 'ASC' ); } return $query; } add_action( 'pre_get_posts', 'sort_query_order' );It worked!! Thank you very much!
This is for plugin´s developers. If there was a pro version to allow sort by expire date, i´d be a customer. This solution works perfect, but hide the products without an expire date and blocks other ways to be ordered. Anywise, Semrocc thank you forever, i don´t have to check every single product any more before they expire.
Regards
Good to hear 🙂
Hi semrocc,
I put your code in functions.php in my child theme and it has no effect. The plugin is great but I still haven’t found a way to reliably sort by the expiration date.
/* Sort posts by expiration */
function sort_query_order( $query ) {
if ( is_admin() && $query->is_main_query() ) {
$query->set( ‘meta_key’,’_expiration-date’ );
$query->set( ‘orderby’,’meta_value_num’ );
$query->set( ‘order’, ‘ASC’ );
}
return $query;
}
add_action( ‘pre_get_posts’, ‘sort_query_order’ );Thats strange…
Delete the code from your functions file and do the following:
1. Create a plugin by inserting the following code into an empty php file (save it as “sort-posts-by-expiration.php”).
2. Upload the file to your plugins folder, activate it and see what happens.
<?php /* Plugin Name: Sort Posts by Expiration Description: Sort admin posts by expiration date. Author: Tom Herod Version: 1.0 Author URI: http://savvyjock.com/ */ function sort_query_order( $query ) { if ( is_admin() && $query->is_main_query() ) { $query->set( 'meta_key','_expiration-date' ); $query->set( 'orderby','meta_value_num' ); $query->set( 'order', 'ASC' ); } return $query; } add_action( 'pre_get_posts', 'sort_query_order' );Works great for the admin panel. Sorted like a champ.
I’m trying to get it to sort my blog posts by expiration date.
I’ll play around with it.
Sounds great 🙂
If you want the results to sort the same way on both the front and back-end of your site, try changing the following code from the plugin:
if ( is_admin() && $query->is_main_query() ) {To:
if ( $query->is_main_query() ) {If you want to create an archive of sorts on your front-end, see my original post..it may point you in the right direction 🙂
That did the trick! I appreciate your help semrocc. You are the man. 🙂
Awesome…thanks!
Hi there,
This method seems to work like a charm for my website back end.
The front end does not work when i replace (it creates an error with none of the posts appearing on the front page):
if ( is_admin() && $query->is_main_query() ) {
To:if ( $query->is_main_query() ) {
Can you please help me out?
Also i noticed the in the images i cannot view the images in the list viewing but i can in the grid viewing. Why is this happening?
The topic ‘Sort by expire date’ is closed to new replies.