I'm using this to list all post titles...
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC'
);
query_posts($args);
if (have_posts()) :
while (have_posts()) :
the_post(); ?>
<p>
<?php the_title(); ?>
</p>
<?php endwhile;
endif;
wp_reset_query();
?>
...and that works fine.
Each of my posts also has a custom field called start_date. All these start dates are entered in the format DD-MM-YYYY (example: 10-03-2016).
What I need to be able to do (and I'm not if this can even be done) is to orderby start_date (instead of title).
To further illustrate, if there were 3 posts with start dates like this...
Post 1: 01-11-2016
Post 2: 03-01-2016
Post 3: 12-07-2016
...the posts would be displayed in the order of....
Post 2
Post 3
Post 1
...because this is in correct date order.
I hope this makes sense.
Cheers.
In wordpress WP_Query class accept args provide more helpful for post filter.
You can check in official site documentation provide custom meta fields accept in query args in order and orderby.
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
I have two methods handle in this situation.
Your date format in start date is not properly mysql date format(YYYY-MM-DD). If you date format will same as mysql format so you need only change query args like:
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'meta_key' => 'start_date',
'post_status' => 'publish',
'orderby' => 'meta_value',
'meta_type' => 'DATE',
'order' => 'ASC'
);
If you do not want to change your date format on every each post or you have many post as difficult to change date format on each post so you can add filter to override order parameters.
Complete code
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'meta_key' => 'start_date',
'orderby' => 'title',
'order' => 'ASC'
);
add_filter( 'posts_orderby', function($orderby){
return 'STR_TO_DATE( wp_postmeta.meta_value, "%d-%m-%Y") ASC';
});
query_posts($args);
if (have_posts()) :
while (have_posts()) :
the_post(); ?>
<p>
<?php the_title(); ?>
</p>
<?php endwhile;
endif;
wp_reset_query();
?>
Hope this helpful for you sorry for poor English.
You can Query and sort posts by custom field value by just adding 'orderby' => 'name_of_your custom_field' to your WP_Query Statement.
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'name_of_your_custom_field',
'order' => 'ASC'
);
Please bear in mind that for example for a date field, the dates must be in "Ymd" format. (2022/12/09).
Hope somebody finds it helpful.
Related
Newbie here...
I have a custom post type of 'equipe' (team in portuguese). I am trying to sort these alphabetically by post title then display the_title so we have a alphabetical list of names.
I've done a search on here and tried a few fixes but Im struggling to get anything other that the standard order.
Any help would be much appreciated!
<?php
$args = array('orderby'=> 'title', 'order' => 'ASC', 'post_type' => 'equipe', 'posts_per_page' => -1, 'post_status' => 'publish' );
$q = new WP_Query($args);
while ( $q->have_posts() ) : $q->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php
endwhile;
wp_reset_query();
?>
<?php
$args = array( 'post_type' => 'equipe', 'posts_per_page'=>5, 'orderby'=>'post_title','order'=>'ASC');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
RESOLVED:
Ok the reason it was enforcing menu_order was because of a setting (F*ing checkbox) within the plugin Post Types Order.
I needed to un-check AUTO SORT
and check Use query ASC / DESC parameter
This then allowed me to adjust the array as follow (and discussed above):
$args = array('orderby' => 'title', 'order'=>'ASC', 'post_type' => 'equipe')
However I did need to add 'order'=>'ASC' into the other pages that sorted by the original query of menu_order.
I recently had a list of events that where in date order and worked fine due to the format being numeric, but the client has since changed the output format and now I'm struggling to list out the events in and Ascending date order, this is the new format:
<?php
$posts = get_posts(array(
'posts_per_page' => 100,
'meta_key' => 'location',
'post_type' => 'events_col',
'offset' => 1,
'orderby' => 'date',
'order' => 'ASC'
));
if( $posts ): ?>
Here is the output:
<div><?php echo date("l dS F Y",strtotime(get_field('date'))); ?></div>
the output is all a bit of a jumble I am not even sure what the order is as is illogical!
I am working in a project where I am using below code to limit number of posts in each category. But I can't figure out how to sort these categories by latest posts. My code is working fine but when I add argument in wp query array, it does not sort or it breaks. I want to order the category with most recent post (date).
Can anyone guide me?
<?php $terms=g et_terms( array( 'taxonomy'=>'category', 'hide_empty' => false, ) ); foreach($terms as $cat){ $cata_name = $cat->name; $term_id = $cat->term_id; ?>
<?php //echo '<h3>'.$catname[0]->cat_name.'</h3>'; ?>
<?php $catqueryy=n ew WP_Query ( 'cat='.$term_id. '&posts_per_page=5');$count=$ catqueryy->found_posts; ?>
You can add ORDER BY argument in wordpress as:
<?php
$args = array(
'post_type' => 'post', //your_post_type
'orderby' => 'date', //meta_value
'meta_query' => array(array('key' => 'cat','value'=>$term_id)),
'order' => 'DESC',
'posts_per_page' => 5,
);
$catqueryy = new WP_Query($args);
?>
You can explore the wordpress document: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
I'm trying to order my custom posts by the most recent date and I can't get it to work. Am I missing something?
<?php
$args = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => '10',
'meta-key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$event_loop = new WP_Query( $args );
if ( $event_loop->have_posts() ) :
while ( $event_loop->have_posts() ) : $event_loop->the_post();
// Set variables
$title = get_the_title();
// Output
?>
<a class="class" href="<?php echo get_permalink(); ?>"><h2><?php echo $title; ?></h2></a>
<img style="float: right; max-width: 28%;"src="<?php the_field('event_image'); ?>"/><h3>Event Date : <?php the_field('event_date'); ?></h3>
'order' => 'ASC' means from the oldest to the newest. Try with
'order' => 'DESC'
Also notice your are ordering on a non date field meta_value_num
Try ordering by a date field that you want to use.
For instance use
'orderby' => 'date'
if your table contains a date for each event. The orderby needs the name of the column with a date type that you want to use to sort the posts/events.
See an example of the wp_posts table here
https://codex.wordpress.org/Database_Description#Table:_wp_posts
An example of a query that works with the wp_posts is shown here
WP_Query('orderby=date&order=DESC')
With use of the Advanced Custom Fields plugin I created a select dropdown which contains 6 membership types. All of my 'listings' using this custom field are assigned one of the 6.
I'd like to display all 'listings' by:
Ultimate Plus
Ultimate
Professional
Commercial
Business
Free
In this particular order, so those paying for the highest level membership have their 'listing' appear at the top of the page.
I expected it to be similar to this which I just found but unsure exactly:
// args
$args = array(
'post_type' => 'directory_listings',
'meta_key' => 'free',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '#',
'value' => array( #, # ),
'compare' => 'IN',
),
),
);
// query
$wp_query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<?php
while( $wp_query->have_posts() ) {
the_post();
ldl_get_template_part('listing', 'compact');
ldl_get_featured_posts();
}
?>
<?php else : ?>
<?php endif; ?>
You are almost there
If you change the choices in the Advanced Custom Fields to
1 : Free
2 : Business
3 : Commercial
4 : Professional
5 : Ultimate
6 : Ultimate Plus
And then the default to 1
By doing this you are setting the values to the number and the default is the Free value. In the admin screen you will be presented with the text value.
Then to do your query try this for the query
$wp_query = get_posts(array(
'numberposts' => -1,
'post_type' => 'directory_listings',
'meta_key' => 'membership_type',
'orderby' => 'meta_value',
));
It will get all of the posts that have a value set and order it by the membership type descending which is what you want.
I have tried this on my local setup to confirm this.