Using get_terms() with meta_query parameters

Inserting boolean term meta values When we add non-existent term meta with e.g. add_term_meta( 123, ‘test’, true ); then we are actually running the following insert : $wpdb->insert( ‘wp_termmeta’, array( ‘term_id’ => 123, ‘meta_key’ => ‘test’, ‘meta_value’ => true ) ); within the general add_metadata() function. Now wpdb::insert() is actually a wrapper for wpdb::_insert_replace_helper() that … Read more

Advanced orderby with multiple meta_query

I tried everything I could to try sorting within the WordPress functions, but was unable to get what I wanted. I ended up following PieterGoosen’s advice and do a usort(). Here is what I have now: WP_Query $args $args = array ( ‘post_type’ => array( ‘member’ ), ‘tax_query’ => array( array( ‘taxonomy’ => ‘group’, ‘field’ … Read more

Wp_query order by multiple custom fields?

$qry_args = array( ‘post_status’ => ‘publish’, ‘post_type’ => ‘event’, // Post type ‘posts_per_page’ => -1, // ALL posts ‘orderby’ => ‘meta_value’, ‘meta_key’ => ‘year’, ‘order’ => ‘DESC’ ); add_filter( ‘posts_orderby’, ‘filter_query’ ); $all_posts = new WP_Query( $qry_args ); remove_filter( ‘posts_orderby’, ‘filter_query’ ); function filter_query( $query ) { $query .= ‘, post_modified DESC’; return $query; } … Read more

How to order a post type with meta_value_num and if meta_value_num does not exist then order by date

You can pass a space-delimited set of columns to orderby argument of WP Query: $args = array( ‘meta_key’ => ‘wp_ratings’, ‘orderby’ => ‘meta_value_num date’ ); $query = new WP_Query( $args ); You can also pass an array of key => sort_order for more granular control, for example: $args = array( ‘meta_key’ => ‘wp_ratings’, ‘orderby’ => … Read more