Multi Sort and Filter WP Query

<?php $filter = $_GET['filter'] ? $_GET['filter'] : null; $args = array( 'posts_per_page' => -1, 'meta_key' => 'is_client', 'orderby' => array( 'meta_value_num' => 'DESC', 'title' => 'ASC' ), 'post_type' => 'projects', 'post_status' => 'publish', 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'our_work_category', 'value' => $post -> ID, 'compare' => 'LIKE', ), array( 'relation' => 'AND', array( 'key' => 'our_work_category', 'value' => $post -> ID, 'compare' => 'LIKE', ), array( 'key' => 'is_client', 'value' => true, 'compare' => '=' ) ) ) ); if($filter) { $args['tax_query'] = array( array( 'taxonomy' => 'project_filters', 'field' => 'slug', 'terms' => $filter ) ); } $projects = new WP_Query($args); ?>
What is this code actually doing though?
1. It will return projects that match a specific project category (post object multi-select field)
2. It will sort the query by whether or not the project is also a client and put those first, then sort the rest alphabetically
3. It has query string parameter support for the variable “$filter”, which will append a tax_query onto the $args while keeping the previous filters and ordering in place.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.