How to make WP get_terms orderby array - wordpress

How can I sort terms using two values?
I'm using this:
$papers = get_terms(
array(
'taxonomy' => 'paper',
'hide_empty' => false,
'orderby' => array(
'year' => 'DESC',
'number' => 'DESC'
),
'order' => 'DESC'
));
But I know get_terms doesn't accept array for orderby.
I've searched a lot but there are not valid solutions or maybe I didn't find the right one.
What can I try?

Use get_terms_orderby filter from wordpress
check these references.
https://developer.wordpress.org/reference/hooks/get_terms_orderby/
http://hookr.io/filters/get_terms_orderby/
https://wp-kama.com/hook/get_terms_orderby

Related

WordPress: WP_Query with posts from a category and meta_query or only another meta_query

I have some trouble with WordPress and the WP_Query.
I would like to get posts filtered by meta_query and/or category, but I have the following problem:
The first type of posts has a custom field named "type" which must be filled with "exercise" and the post has to be in a category named "Level" (this will be set before).
The second type of posts has only the custom field named "type" which must be filled with "test".
I don't know how to get these two conditions together.
Because of that I've tried to split it into two Queries, and merge it afterward, like this:
$firstArgs = array(
'posts_per_page'=> -1,
'category_name' => $level,
'meta_key' => 'duration',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'key' => 'type',
'value' => 'exercise'
)
);
$secondArgs = array(
'posts_per_page' => -1,
'meta_key' => 'duration',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'key' => 'type',
'value' => 'test'
)
);
$first_query = new WP_Query( $firstArgs );
$second_query = new WP_Query( $secondArgs );
$result = new WP_Query();
$result->posts = array_merge($first_query->posts, $second_query->posts);
The Problem with this method is, that I would like to sort the posts by the custom field "duration" DESC. If I merge these two arrays, the sorting isn't that way I would like to.
Does anyone know a better way of doing this? If it would be one query, the sorting would work and I think it is more efficient.
Thanks for your help!
$firstArgs = array(
'posts_per_page'=> -1,
'category_name' => $level,
'meta_key' => 'duration',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'key' => 'type',
'value' => 'exercise'
)
);
$secondArgs = array(
'posts_per_page' => -1,
'meta_key' => 'duration',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'key' => 'type',
'value' => 'test'
)
);
$firstArgs = get_posts($firstArgs);
$secondArgs = get_posts($secondArgs);
$your_posts = array_unique(array_merge($firstArgs,$secondArgs),SORT_REGULAR);
_e('<pre>');
print_r($your_posts);
_e('</pre>');
For your better understanding visit here
I found an answer that worked for me.
First of all, I retrieve the data as Dhruv told me and then, I sorted the result with the usort-function, but with an anonymous function.
usort($your_posts, function($a, $b)
{
$val1 = get_post_meta($a->ID, 'duration', true );
$val2 = get_post_meta($b->ID, 'duration', true );
return -1*($val1 - $val2); // sort desc
});
Maybe it is not the best method, but it worked for me. :-)

Multiple custom field sorting on wordpress archive page

I have been through several topics on sorting taxonomies and custom fields, including some that were promising. However, I am still not constructing my query args correctly it seems.
I am "fixing" someone else's code in a custom theme that is used in a multisite configuration, but I am able to override/filter the query on a per-site basis.
The query is called via the theme with the following:
return apply_filters( 'theme_query_args_filter', $args);
I have custom fields in the "role" taxonomy that include:
last_name
first_name
Pretty common I think.
However, when the args are executed, the following filter args are sorting only by last name (key part is the else clause):
function my_args_filter( $args ) {
if (is_home() && !is_search()) {
$args['meta_key'] = 'event_date';
$args['orderby'] = 'event_date';
$args['order'] = 'DESC';
}
else {
$tax = $args['taxonomy'];
$theterm = $args['term'];
$args = array (
'taxonomy' => $tax,
'term' => $theterm,
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC'
);
}
add_filter( 'theme_query_args_filter', 'my_args_filter' );
I've tried to modify the orderby as indicated in https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/ to use an array to do a multisort, but I'm hitting my head up against the wall. I think the problem is that the code is written using a mixture of old ways of doing things and new ways.
Any advice is appreciated. According to the example on in the docs above, I SHOULD be able to pass in multiple meta key/value/orders via an array, but I'm just not getting it.
Thanks for any leads you might have. (long-time listener, first-time caller)
(I also looked at https://wordpress.stackexchange.com/questions/109849/order-by-desc-asc-in-custom-wp-query but I couldn't extrapolate that example to this one either)
Figured it out on my most recent try. Hat tip to this solution https://wordpress.stackexchange.com/questions/249881/multiple-custom-fields-for-orderby-in-wp-query by Nath. if there is a more elegant way to do it, please let me know.
$tax = $args['taxonomy'];
$theterm = $args['term'];
$paged = $args['paged'];
$args = array (
'taxonomy' => $tax,
'term' => $theterm,
'paged' => $paged,
'posts_per_page' => '10',
'meta_query' => array(
array(
'relation' => 'AND',
'last_name' =>
array(
'key' => 'last_name',
'compare' => 'EXISTS',
),
'first_name' =>
array(
'key' => 'first_name',
'compare' => 'EXISTS',
),
),
),
'orderby' => array (
'last_name' => 'ASC',
'first_name' => 'ASC',
)
);
}

Can't get tax_query to work

I have a custom WP Query where I list custom post types. I am trying to set up filters to filter listings by custom taxonomy.
My query setup is as follows:
// WP_Query arguments
$args = array (
'post_type' => 'opps',
'post_status' => 'publish',
'pagination' => true,
'posts_per_page' => $postsperpage,
'posts_per_archive_page' => $postsperpage,
'ignore_sticky_posts' => true,
'order' => 'DESC',
'orderby' => 'date',
'cache_results' => true,
'update_post_meta_cache' => true,
'update_post_term_cache' => true,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'sector',
'field' => 'slug',
'terms' => 'business-services'
),
)
);
So in the above example I should get only posts with taxonomy 'sector' with slug 'business-services', but ALL posts from custom post types 'opps' get listed instead.
I have looked everywhere on stackoverflow and beyond but I just can't figure out what I've been doing wrong.
Your code looks fine.
I have had this issue before because wordpress does not sync taxonomy terms until late in the init phase. Have you tried running this code in a template, after get_header() or wp_head() has been called? If not try that, it should work then. I know if I've echoed it out in functions.php for testing it will not work because wordpress has not synced tax terms yet.
You should really have a look at the codex (WP_Query) when creating custom queries. As your code stands, it will fail. You have the following issues
pagination is not a valid parameter for WP_Query.
You cannot use posts_per_page and posts_per_archive_page together
You don't need to set parameter values that are set by default. Unnecessary parameters used in your code which is by default the default values are order, orderby, post_status, cache_results, update_post_meta_cache and update_post_term_cache
There is no sticky post functionality for custom post types. just remove the ignore_sticky_post parameter
Your code should look something like this ( I hope your variables is defined )
// WP_Query arguments
$args = array (
'post_type' => 'opps',
'posts_per_page' => $postsperpage,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'sector',
'field' => 'slug',
'terms' => 'business-services'
),
)
);

Wordpress sort by meta_value not working with meta_query

I have a template that pulls a custom post type for properties and sorts them by state. The script works properly until you add in a meta_query for the agent. It pulls the correct properties, but it doesn't sort them by state with the meta_query present. Here's the query:
$qry = array(
'post_type' => array( 'practices-tpsg' ),
'meta_key' => 'wpcf-practice-state',
'orderby' => 'meta_value',
'order' => 'asc',
'showposts' => 18,
'paged' =>$paged
);
if($_GET['agent'])
{
$qry['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'wpcf-agent',
'value' => $_GET['agent']
),
array(
'key' => 'wpcf-agent2',
'value' => $_GET['agent']
),
);
}
Does anyone know why it would stop sorting properly after the meta_query is added to the query?
Here's a link to the page: http://www.totalpracticesolutionsgroup.com/practices-for-sale/ The sort order works properly in the default view, which allows the state name headers above the listings. If you select an agent from the drop down in the top right, the correct properties are pulled, but they are no longer sorted by state. You can also see the output of the query by adding &debug=1 to the end of the url.
replace 'orderby' => 'meta_value' with 'orderby' => 'meta_value_num

Search for posts by title using WP_Query

May I know whether the query below is correct? I would like to search for post that contain the title 'hello' using meta_query with various variables
$sq = new WP_Query(array( 'meta_query' => array(array('key' => 'post_title', 'value' => array('%hello%'), 'compare' => 'LIKE')), 'post_type' => 'post', 'posts_per_page' => $num, 'post_status' => 'publish', 'order' => $order, 'orderby' => $orderby, 'ignore_sticky_posts' => 1, 'cat' => $cats, 'offset' => $offset));
Thanks.
get_page_by_title() is usually NOT case sensitive. It only would be if the database was set to be case sensitive, which is unusual for MySQL.
Instructions for use of get_page_by_title() can be found at: developer.wordpress.org
Be sure to set the third parameter if you are wanting posts and not pages.

Resources