Sort posts by custom field number - wordpress

I'm using this query do get some posts.
$args = array(
'post_type' => 'spaces',
'post_per_page' => '500',
'orderby' => 'rand',
'meta_key' => 'space-city',
'meta_value' => $search,
);
$query = new WP_query($args);
Now I need to order the results by total of comments on each post. I've a custom field with this number called "space-comments", but I've no idea how to sort this posts with this second meta_key.
I made some tests, but I only was able to get post when "space-comments" has a value. When there is no value, the post don't show up.
Any ideia how can I start?

The WP_Query can accept a meta_query argument that is populated as an array of sub-arguments. That array can have sub arrays that are each their own meta query, so you can create some nice compound searches across meta data.
See the example from https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters below.
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE',
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
);
$query = new WP_Query( $args );

Related

Wordpress get_posts not working as expected

I'm trying to retrieve posts in Wordpress using the get_posts function, trying to filter by a custom field named cegep_region but the method is returning posts with any value in this field. My query below:
$cegep = get_posts(array(
'post_type' => 'cegep',
'orderby' => 'rand',
'posts_per_page' => -1,
'meta_query' => array(
'key' => 'cegep_region',
'value' => '386',
'compare' => '='
)
));
When I look into the database, 386 is not what there is in the meta_value.
select * from wp_postmeta where post_id=577 and meta_key='cegep_region'
What can I possibly be doing wrong?
Try without using compare and add one more array inside in meta_query
$args = array(
post_type' => 'cegep',
'meta_query' => array(
array(
'key' => 'cegep_region',
'value' => 386,
)
)
);
$postslist = get_posts( $args );
its work fine with me

WP Query: How to get posts from a specific author OR those with a specific meta value

I would like to retrieve all those posts whose author (post table field) is a given one OR those which has a given meta value (postmeda table field).
If "author" was a meta value, I know I could use a meta_query to achieve it. The thing here is that it is not... so I think I cannot use the "author" field within a meta_query and use the "relation" key.
I'm looking for something like:
$args = array(
'post_type' => array('post'),
'orderby' => 'ASC',
'order' => 'date',
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'field' => 'author',
'value' => $author_id,
'compare' => '==',
),
array(
'key' => '_meta_field_name',
'compare' => 'NOT EXISTS',
),
),
array(
'relation' => 'AND',
array(
'key' => '_meta_field_name',
'compare' => 'EXISTS',
),
array(
'key' => '_meta_field_name',
'value' => $meta_field_value,
'compare' => '==',
),
),
),
);
$data = new WP_Query( $args );
Any suggestion on how to achieve that using WP_Query?
Thanks!
You might like to try an approach like this one instead. The idea is to query the two conditions you want to search for, then merge the two queries into one finished product.
//Get posts with the author you're looking for
$args1 = array(
'author_name' => 'testuser', //or 'author' => $author_id or something else
);
$data1 = get_posts( $args1 );
//Get posts with the meta data you're looking for
$args2 = array(
'meta_query' => array(
array(
'key' => 'meta_field_name',
'compare' => 'EXISTS',
),
array(
'key' => 'meta_field_name',
'value' => $meta_field_value,
'compare' => '==',
),
),
);
$data2 = get_posts( $args2 );
//Merge both arrays
$allData = array_merge( $data1, $data2 );
//Get just the IDs of all the posts found, while also dropping any duplicates
$postIDs = array_unique( wp_list_pluck( $allData, 'ID' ) );
//Do a new query with these IDs to get a properly sorted array of post objects
$args3 = array(
'post__in' => $postIDs,
'order' => 'ASC',
'orderby' => 'date',
);
$finalAnswer = get_posts( $args3 ); //This is your array of post objects. Ta-Da!

query posts by ACF field not working

There are plenty of topics related to mine, but I still haven't found a solution. I'm trying to query posts by ACF field (radio button) and it seems that the meta_query gets completely ignored. It returns all the posts, instead of only those matching the criteria. I have tried using the field key instead of field name, other comparisons, etc. nothing seems to work. Hope you have an idea of what may be wrong! Here's my code:
<?php
$post_args = array(
'post_type' => 'products',
'posts_per_page' => - 1,
'status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'meta_key' => 'product_taste',
'meta_value' => array( 'cold' ),
'compare' => 'IN',
),
array(
'meta_key' => 'product_served',
'meta_value' => array( 'grated' ),
'compare' => 'IN'
)
),
);
$query = new WP_Query( $post_args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : ?>
<?php
$query->the_post();
?>
<h5>
<?php the_title(); ?>
</h5>
<?php endwhile ?>
<?php wp_reset_postdata();
}
?>
Use 'key' and 'value' in the 'meta_query' array
You don't need to use meta_key and meta_value in a meta_query... you only use those directly in the $args array. If you are adding a meta_query array, you just use key and value, e.g.
$post_args = array(
[...]
'meta_query' => array(
array(
'key' => 'product_taste',
'value' => 'cold',
'compare' => 'LIKE',
),
[...]
2. Querying serialised data with an array of values
There can also be issues using 'compare' => 'IN' with an array of values, when you are trying to query ACF data, because the ACF data can be serialised in the database (e.g. if data is in a repeater).
As you will only ever search for a single value, you can use LIKE instead of IN.
Putting these together
$post_args = array(
'post_type' => 'products',
'posts_per_page' => - 1,
'status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'product_taste',
'value' => 'cold',
'compare' => 'LIKE',
),
array(
'key' => 'product_served',
'value' => 'grated',
'compare' => 'LIKE'
)
),
);
$query = new WP_Query( $post_args );
If the data is serialized and you have values that could return multiple matches (e.g. LIKE 'cold' would match words like "cold", "colder","coldest"), then try adding a semicolon (;) at the end of the value, e.g.
[...]
array(
'key' => 'product_taste',
'value' => 'cold;', // add ; to the end of the value
'compare' => 'LIKE',
),
array(
'key' => 'product_served',
'value' => 'grated;', // add ; to the end of the value
'compare' => 'LIKE'
)
[...]
This will work when the values are serialised in the database, because each item will be separated by a semicolon.

Wordpress get posts with multiple meta values

I have a ACF select field which takes multiple value. Now I want to use get_posts() to get those custom posts. My arguments look like this:
$party = 'test1';
$function = 'test1, test2';
$args = array(
'numberposts' => -1,
'post_type' => 'event',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'party',
'compare' => '=',
'value' => $party,
),
array(
'key' => 'function',
'compare' => 'IN',
'value' => array($function),
)
)
);
$items = get_posts($args);
But this does not work! Don't know what is wrong here!

How to sort multiple wordpress custom field values?

Display posts with 'Product' type ordered by 'Price' custom field:
$query = new WP_Query(
array ( 'post_type' => 'product',
'orderby' => 'meta_value',
'meta_key' => 'price' )
);
Which code should I use if also want to order by 'Size'?
Another example on which I need multiple sort on custom fields:
Display posts with 'Event' type ordered by 'Start_Hour' and then by 'Start_Minute'.
Thanks to Bainternet I found the solution:
function orderbyreplace($orderby) {
return str_replace('menu_order', 'mt1.meta_value, mt2.meta_value', $orderby);
}
and...
$args = array(
'post_type'=>'Events',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'Start_Hour',
'value' => '',
'compare' => 'LIKE'
),
array(
'key' => 'Start_Minute',
'value' => '',
'compare' => 'LIKE'
)
)
);
add_filter('posts_orderby','orderbyreplace');
$loop = new WP_Query( $args );
remove_filter('posts_orderby','orderbyreplace');
I think this changed slightly in Wordpress 3.7.
I had to change
return str_replace('menu_order', 'mt2.meta_value, mt1.meta_value', $orderby);
into
return str_replace('wp_posts.menu_order', 'mt2.meta_value, mt1.meta_value', $orderby);
Thanks for the initial solution! [Edited]
Here's an example of using more than one meta_key and orderby that I believe should work:
$params = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'price',
'value' => '',
'compare' => 'LIKE'
),
array(
'key' => 'size',
'value' => '',
'compare' => 'LIKE'
)
),
'orderby' => 'price size',
'order' => 'ASC'
);
$query = new WP_Query;
$resulting_obj = $query->query($params);
You'll need to play with the meta_query items a bit more, especially the 'value' parameter. Please have a good look at http://codex.wordpress.org/Class_Reference/WP_Query in the 'Custom Field Parameters' section.
You don't need any filter or hooks to sort multiple custom fields, if your one of custom field is meta_key and other one is normal column of table, than use these parameters/arguments in your query.
'meta_key' => 'KEY_NAME',
'orderby' => 'meta_value_num SECOND_COLUMN_NAME',
'order' => 'ASC' or 'order' => 'DESC'
Here the order of meta_value_num and SECOND_COLUMN_NAME matter, you may see different-2 result based on the order.

Resources