Order by post_meta in wp_query wordpress not working - wordpress

I want to order posts by price in wordpress, I tried a lot and also concerned by documentation, all looks good but still it is not working..
here is code....
global $wp_query;
$query_vars = $wp_query->query_vars;
$post_per_page = 12;
global $term;
$term = (strip_tags($_GET['term']));
if (!empty($_GET['term'])) {
add_filter('posts_where', 'taskerdev_posts_where');
}
$meta_query = array();
$closed = array(
'key' => 'closed',
'value' => "0",
'compare' => '='
);
$meta_query[] = $closed;
if (!empty($_GET['tasker_cat_cat']))
$tasker_cat = array(
'taxonomy' => 'tasker_cat',
'field' => 'slug',
'terms' => $_GET['tasker_cat_cat']);
$meta_query[] = $tasker_cat;
$price = array(
'meta_key' => 'price',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$meta_query[] = $price;
$args = array('post_type' => 'shoping', 'posts_per_page' => 10,
'paged' => $query_vars['paged'], 'meta_query' => $meta_query);
This code looks fine, but I cant get post order by price that is in post_meta..
I can see this code to sort results every where even in documentation of wp_query.
$price = array(
'meta_key' => 'price',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$meta_query[] = $price;

are you sure that the metafield price is numeric?
'orderby' => 'meta_value_num',
works only on numeric fields.. if a value is numeric depends on the format of price saved in your field.
maybe post a example value so that people see what the real value is

Related

Exclude featured posts through custom query not working

I am trying to exclude posts from query and it is not working at all.
Here what I tried
<?php
$args = array(
'post_type' => 'videos-presentations',
'post_status' => 'publish',
'posts_per_page' => 4,
'paged' => $paged,
'meta_query' => array(
array(
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes',
'meta_compare' => '!='
)
)
);
$my_query = new WP_Query($args);
?>
Also Tried with
'meta_compare' => 'NOT EXIST'
and
'meta_compare' => 'NOT IN'
Any idea what I am doing wrong?
Got it. From here
It Works with just
'meta_query' => array(
array(
'key' => '_is_ns_featured_post',
'compare' => 'NOT EXISTS'
)
)
function exclude_posts ( $query ) {
$meta_query = $query->get( 'meta_query' );
$meta_query[] = array(
'key'=>'_is_ns_featured_post',
'value'=>'yes',
'compare'=>'!=',
);
$query->set( 'meta_query',$meta_query );
}
add_action( 'pre_get_posts', 'exclude_posts' );
place this code in functions.php file of active theme

custom wp_query where meta_key is variable or not needed

To make a custom post type filterable and sortable, i created a custom query that works based on variables in url, accessed by $_GET.
One specific situation breaks it. This is an (un)specified metakey. In the case of sort, the metakey is needed when sorting on a custom field. In the other cases, the metakey can be ignored. However, when i set the variable on empty, the query doesnt produce any posts. How to deal with an empty meta_key?
So far i tried to set the variable as empty ($variable ='');
I set the variable as null; I have used unset.
if (isset($_GET["key"]) || isset($_GET["orderby"])){
if (isset($_GET["key"])) {$key = $_GET["key"];}
else {$key = '';}
if (isset($_GET["value"])) {$value = $_GET["value"]; echo $value;}
else {$value = '';}
if (isset($_GET["orderby"])) {$orderby = $_GET["orderby"];}
if ($orderby='meta_value'){$meta_key='averagerating';}
else {$orderby='';$meta_key='';}
if (isset($_GET["order"])) {$order = $_GET["order"];}
else {$order='';}
$cat = get_queried_object();
if (!empty($cat) && !is_post_type_archive('bedrijf')){
$category = $cat->name;
}
if (is_post_type_archive('bedrijf')){
$category = '';
$terms = get_terms( 'bedrijfs-category' );
// convert array of term objects to array of term IDs
$category = wp_list_pluck( $terms, 'name' );
}
global $post;
$the_query ='';
$args = array(
'post_type' => 'bedrijf',
'posts_per_page' => 10,
'meta_key' => ''.$meta_key.'',
'orderby' => ''.$orderby.'',
'order' => ''.$order.'',
'tax_query' => array(
array(
'taxonomy' => 'bedrijfs-category',
'field' => 'name',
'terms' => $category
)
),
'meta_query' => array(
array(
'key'=> ''.$key.'',
'value' => ''.$value.'',
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query($args); }
WHat i would like is a solution for inserting an empty variable in the meta_key => ''.$meta_key.'' so the loop skips the meta_key part.
define meta_query or tax_query out of $args
and (example):
if($_GET['some_name'] !== ''){
$meta_query = array(
array(
'taxonomy' => 'bedrijfs-category',
'field' => 'name',
'terms' => $category
)
);
//define args with some_one para exists
$args = array(
'post_type' => 'bedrijf',
'posts_per_page' => 10,
'meta_key' => ''.$meta_key.'',
'orderby' => ''.$orderby.'',
'order' => ''.$order.'',
'meta_query' => $meta_query
);
}else{
//define args with some_one para not exists
$args = array(
'post_type' => 'bedrijf',
'posts_per_page' => 10,
'meta_key' => ''.$meta_key.'',
'orderby' => ''.$orderby.'',
'order' => ''.$order.''
);
}
tax_query is like this

Ordering by two custom fields values

I have custom post type called "lawyer" and I want to query lawyers ordered by custom field "surname" and then ordered by custom field "name". My query looks like this:
query_posts( array ( 'post_type' => 'lawyer', 'posts_per_page' => -1, 'meta_query' => array(array('key' => 'surname', 'orderby' => 'meta_value', 'order' => ASC), array('key' => 'name', 'orderby' => 'meta_value', 'order' => ASC) )) );
But it's not working. I get results, but they are not ordered the way I want them to. Any idea what is wrong here?
Don't use query_posts() ever.
Reference When should you use WP_Query vs query_posts() vs get_posts()?
You can do it by using WP_QUERY and adding filter to posts_orderby
function custom_orderby($orderby) {
global $wpdb;
return str_replace($wpdb->prefix.'postmeta.meta_value', 'mt1.meta_value, mt2.meta_value ASC', $orderby);
}
Then Prepare your arguments.
$args = array(
'post_type'=>'lawyer',
'orderby' => 'meta_value',
'meta_key' => 'surname',
'meta_query' => array(
array(
'key' => 'surname',
'value' => '',
),
array(
'key' => 'name',
'value' => '',
)
)
);
And add your custom order by filter just before the query execute.
add_filter('posts_orderby','custom_orderby');
$loop = new WP_Query( $args );
remove_filter('posts_orderby','custom_orderby');
Finally made it work with this code:
function double_meta_posts_orderby($orderby) {
remove_filter('posts_orderby', 'double_meta_posts_orderby');
global $wpdb;
return " {$wpdb->postmeta}.meta_value, mt1.meta_value";
}
$args = array(
'post_type'=>'lawyer',
'post_per_page' => -1,
'meta_query' => array(
array(
'key' => 'surname',
),
array(
'key' => 'name',
)
),
'orderby' => 'meta_value',
'order' => ASC,
);
add_filter('posts_orderby', 'double_meta_posts_orderby');
$loop = new WP_Query( $args );
// the Loop
while ($loop->have_posts()) : $loop->the_post();
...

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.

Wordpress: show future events based on custom field AND order by date

I have a custom content type (event), within that type I have a custom date field, with the name: 'date'. In my template I am running the WP_Query to return a list of events. I would like to only get events that are in the future (according to my custom 'date' field) and then order the list by that same field.
I have tried the following but it simply returns a list of all the 'events' in the system, regardless of date
$today = date('d M, y');
$args = array (
'post_type' => 'event',
'meta_query' => array(
array(
'key' => 'date',
'value' => $today,
'compare' => '>',
'type' => 'CHAR',
),
),
'meta_key' => 'date',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
As a note: if I replace the type from 'CHAR' to 'DATE' I get no results returned...
Try this:
functions.php
function custom_unixtimesamp ( $post_id ) {
if ( get_post_type( $post_id ) == 'events' ) {
$startdate = get_post_meta($post_id, 'event_date_begins', true);
if($startdate) {
$dateparts = explode('/', $startdate);
$newdate1 = strtotime(date('d.m.Y H:i:s', strtotime($dateparts[1].'/'.$dateparts[0].'/'.$dateparts[2])));
update_post_meta($post_id, 'unixstartdate', $newdate1 );
}
}
}
add_action( 'save_post', 'custom_unixtimesamp', 100, 2);
then query
$today = time();
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => '10',
'meta_query' => array(
array(
'key' => 'unixstartdate',
'compare' => '>=',
'value' => $today,
)
),
'meta_key' => 'event_date_begins',
'orderby' => 'meta_value',
'order' => 'ASC',
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
);

Resources