From 2665d4a13d232862a3d5aacd624783396844d5af Mon Sep 17 00:00:00 2001 From: lavolpecheprogramma Date: Mon, 31 Aug 2020 10:09:36 +0200 Subject: [PATCH] add taxonomy type field to posts query --- Aeria/Query/Query.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Aeria/Query/Query.php b/Aeria/Query/Query.php index f02f36c..656a4f9 100644 --- a/Aeria/Query/Query.php +++ b/Aeria/Query/Query.php @@ -207,6 +207,7 @@ public function getPosts($parameters) $parentID = (isset($parameters['parent_id'])) ? $parameters['parent_id'] : null; $taxonomy = (isset($parameters['taxonomy'])) ? $parameters['taxonomy'] : null; $taxonomyTerms = (isset($parameters['taxonomy_terms'])) ? $parameters['taxonomy_terms'] : null; + $taxonomyField = (isset($parameters['taxonomy_field'])) ? $parameters['taxonomy_field'] : 'term_id'; $orderBy = (isset($parameters['orderby'])) ? $parameters['orderby'] : null; $order = (isset($parameters['order'])) ? $parameters['order'] : null; $numberPosts = (isset($parameters['numberposts'])) ? $parameters['numberposts'] : -1; @@ -231,16 +232,21 @@ public function getPosts($parameters) 's' => $searchField, 'post_type' => $postType, 'post_parent' => $parentID, - 'tax_query' => [ - [ - 'taxonomy' => $taxonomy, - 'terms' => $taxonomyTerms - ] - ], 'orderby' => $orderBy, 'order' => $order, 'numberposts' => $numberPosts, ]; + + if (isset($taxonomy) || isset($taxonomyTerms)) { + $args['tax_query'] = [ + [ + 'taxonomy' => $taxonomy, + 'slug' => $taxonomyField, + 'terms' => $taxonomyTerms, + ], + ]; + } + $posts = get_posts($args); $response = []; foreach ($posts as $index => $post) {