Wordpress Query - Order by meta_value_num AND date - wordpress

im a bit stuck at the moment with the order in my wp_query.
I want to sort a query firstly by meta_value_num (this works perfect) AND as fallback by the date. But in my query the date seems dominant in comparision with the meta_value_num.
So it sorts all my posts by date and then apllys the meta_num_value order and not vice versa.
Do you have any clue how to do this?
$args = array(
'post_type' => 'anbieter',
'showposts' => -1,
'order' => 'DESC',
'orderby' => 'meta_value_num date',
'meta_key' => 'rating'
);
I found so many threads to order by two custom fields but not to sort by by "normal post field" AND custom field.
regards,

I found a solution a while ago which was this. Add this to your functions.php:
function wdw_query_orderby_postmeta_date( $orderby ){
$new_orderby = str_replace( "wp_postmeta.meta_value", "STR_TO_DATE(wp_postmeta.meta_value, '%d-%m-%Y')", $orderby );
return $new_orderby;
}
Then do this with your query:
add_filter( 'posts_orderby', 'wdw_query_orderby_postmeta_date', 10, 1);
$args = array(
'post_type' => 'anbieter',
'showposts' => -1,
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => 'rating'
);
remove_filter( 'posts_orderby', 'wdw_query_orderby_postmeta_date', 10, 1);

Related

Wordpress output links to previous and next posts from custom query

I'm using the advanced custom fields plugin for wordpress to create a group of custom post types that have a date set within them.
I'm trying to show the previous post, and the next post, based on the date stored in the custom field. The links need to link to posts that have a date set in the future (so don't show links to posts with dates that have gone by)/
I can get a list of all the posts that are in the future, and out put these using the following code;
<?php
$rightnow = current_time('Ymd');
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array(
array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
)
),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
?>
Output details of post here....
<?php
}
?>
What I thought I could do, is the get the current post's position in the array, to then get details of the posts one before and one after... but I haven't got a clue how to do this.
I've experimented with the wordpress next_post_link and previous_post_link functions, but these seem to work based on when the post was added to wordpress, rather than based on my custom date field.
Am I going about this the complete wrong way? Any tips or pointers would be much appreciated!
Use WP_Query plus paginate_links
$rightnow = current_time('Ymd');
// Query Args
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array( array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
) ),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$query = new WP_QUery( $arg );
$posts = $query->get_posts();
// Paginate Args
$page_args = array(
'base' => 'your_custom_page_url'.'%_%', // Make sure you got this current depending on your setup
'format' => '/%#%', // requires pretty permalinks
'total' => $query->max_num_pages,
'current' => 0,
'prev_text' => __('«'),
'next_text' => __('»'),
);
foreach ( $posts as $post ) {
// Output
}
echo paginate_links( $page_args );
You have to verify that the base and format of paginate args are correct of it won't properly worked.

Woocommerce - Custom sorting and then ABC order

I have code that I am modifying inside of Up-Sells.php
I want to order by using a custom meta (OrderForProduct) and then I want to sort by ABC order. Does anyone know how to do this?
Right now when I add the meta query sorting, the products do not show if they do not have this custom value. I am looking to have it sort by the custom value and then have it sort by ABC order.
$meta_query = WC()->query->get_meta_query();
$args = array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'meta_key' => 'OrderForProduct',
'posts_per_page' => 50,
'orderby' => 'meta_value_num', 'order'=>'ASC',
'post__in' => $upsells,
'post__not_in' => array( $product->id ),
'meta_query' => $meta_query
);
$products = new WP_Query( $args );
What does "ABC order" mean? Are you talking about sorting by the product title? If so, I think you can do this:
'orderby' => 'meta_value_num title',
Although I've never tried it, that's how I understand it from the docos.
http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

WordPress meta_query for Custom Post Type with orderby

I'm trying to sort out a WordPress query for a Custom Post Type, but I can't make it work.
The post type is events. An Advanced Custom Fields field called sticky (yes/no) is used for sorting (stickies on top), and another ACF field called glamrock (yes/no) is used to exclude certain posts.
The result is, glamrock gets excluded, but stickies are not sorted.
If I delete the meta_query, sorting works fine, but glamrock posts are included.
$bpb_args = array(
'numberposts' => -1,
'post_type' => 'events',
'posts_per_page' => 100,
'paged' => get_query_var( 'paged', true ),
'meta-key' => 'sticky',
'meta_query' => array(
array(
'key' => 'glamrock',
'value' => 'no',
'compare' => 'IN',
)
),
'orderby' => 'meta_value',
'order' => 'ASC',
);
$bpb_query = new WP_Query( $bpb_args );
if( $bpb_query->have_posts() ):
while( $bpb_query->have_posts() ) : $bpb_query->the_post();
//show post
endwhile;
endif;
Update:
Unfortunately, meta_value instead of meta_value_num didn't change anything. It still seems to be sorting by date/time.
The Advanced Custom Field type is Radio Buttons.
In addition to the arguments, I also included the loop.
You need to specify only meta_value since your meta-key is non numeric
'orderby' => 'meta_value'
#Mihai had it right: meta_value_num was the main issue. I changed it to meta_value.
Here's the query/loop that worked:
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => 'sticky',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'lizenz_erteilt',
'value' => 'no',
'compare' => 'LIKE',
),
)
);
$query = new WP_Query( $args );
// Loop
if( $query->have_posts() ) :
while( $query->have_posts() ) : $query->the_post();
//show post
endwhile;
endif;

How compare custom fields date in wordpress?

I am doing a task for event management, I have coded a widget to display Coming or Past events in sidebar, but I can not handle the custom fields as date. following is my code, while the date is being stored as "m/d/Y". Please help me to resolve this issue. Thanks in adance
$today = date('m/d/Y');
$args = array(
'post_type' => 'event',
'post_status' => 'publish',
'meta_key' => 'event_date',
'posts_per_page' => '5',
'meta_query' => array(
array(
'key' => 'event_date',
'value' => $today,
'compare' => '<=',
'type' => 'date'
)
),
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
Date must be in YYYY-MM-DD format for the meta_query comparison to work
$today = date("Y-m-d");
While I agree with the previous answer (you should store dates in YYYY-MM-DD format), I thought I could find a workaround using a Wordpress filter, so here you go.
This should go into your functions.php file:
add_filter('posts_where', 'my_callback', 10, 2);
function my_callback( $where, $wp_query_obj ) {
if( isset( $wp_query_obj->query['date_format'] ) ) {
$where = preg_replace('~CAST\((.+?)\)~', "STR_TO_DATE(CAST($1), '{$wp_query_obj->query['date_format']}')", $where);
}
return $where;
}
And this is your query:
$today = date('m/d/Y');
$args = array(
'post_type' => 'event',
'posts_per_page' => '5',
'meta_key' => 'event_date',
'meta_value' => $today,
'meta_compare' => '<=',
'date_format' => '%m/%d/%Y'
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
The key here is the date_format argument I added to the query: if you don't add it the filter won't be applied.
This could work with other date formats, you only need to change the date_format argument consistent with the STR_TO_DATE MySQL function parameter format.

Obtain post by id without order in wordpress

I am try to get this page by ids like this.
$args = array(
'post_type' => 'page',
'post__in' => array(2,1220,3731,696,1899,380)
);
$query = query_posts($args);
this is ok, but I obtain the post by order first 3731 and last 2, buy I don't want any order I want this order, 2, 1220, 3731, 696... etc
any idea!!
add the orderby parameter
$args = array(
'post_type' => 'page',
'post__in' => array(2,1220,3731,696,1899,380)
'orderby' => 'post__in',
);
$query = query_posts($args);
CODEX
Hi #Mark thanks for all I try this too but no work.
Finally I found the solution!
$args = array(
'post_type' => 'page',
'meta_key' => 'ordenacion',
'orderby' => 'meta_value',
'order'=>'ASC',
'post__in' => array(2,1220,3731,696,1899,380)
);
$query = query_posts($args);
I create a field custom and order by the value of meta_key

Resources