From af9d7c81e88dfbfb6899ef32e2ba8f6700174bfd Mon Sep 17 00:00:00 2001 From: eduardohumberto Date: Tue, 27 Mar 2018 23:32:17 -0300 Subject: [PATCH] remove TODO in category filter type and allow filter by description --- src/api/class-tainacan-rest-controller.php | 8 ++++++- .../category/class-tainacan-category.php | 1 - .../repositories/class-tainacan-items.php | 23 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/api/class-tainacan-rest-controller.php b/src/api/class-tainacan-rest-controller.php index 41dea6cf5..f3eb0adb1 100644 --- a/src/api/class-tainacan-rest-controller.php +++ b/src/api/class-tainacan-rest-controller.php @@ -259,12 +259,18 @@ class TAINACAN_REST_Controller extends WP_REST_Controller { // handle core field if( array_key_exists("key", $a) ){ $field = new Tainacan\Entities\Field($a['key']); - if( strpos( $field->get_field_type(), 'Core') !== false ){ + if( strpos( $field->get_field_type(), 'Core_Title') !== false ){ $args[ 'post_title_in' ] = [ 'relation' => ( isset( $request_meta_query['relation']) ) ? $request_meta_query['relation'] : 'AND' , 'value' => ( is_array( $a['value'] ) ) ? $a['value'] : [$a['value']] ]; continue; + } else if( strpos( $field->get_field_type(), 'Core_Description') !== false ) { + $args[ 'post_content_in' ] = [ + 'relation' => ( isset( $request_meta_query['relation']) ) ? $request_meta_query['relation'] : 'AND' , + 'value' => ( is_array( $a['value'] ) ) ? $a['value'] : [$a['value']] + ]; + continue; } } diff --git a/src/classes/field-types/category/class-tainacan-category.php b/src/classes/field-types/category/class-tainacan-category.php index 3afd5ab82..d09000f01 100644 --- a/src/classes/field-types/category/class-tainacan-category.php +++ b/src/classes/field-types/category/class-tainacan-category.php @@ -23,7 +23,6 @@ class Category extends Field_Type { ]); $this->set_form_component('tainacan-form-category'); - // TODO: Set component depending on options. If multiple is checkbox. if single, select. etc. $this->set_component('tainacan-category'); } diff --git a/src/classes/repositories/class-tainacan-items.php b/src/classes/repositories/class-tainacan-items.php index fe3ccbe9b..2068e53c9 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -26,6 +26,7 @@ class Items extends Repository { { parent::__construct(); add_filter( 'posts_where', array(&$this, 'title_in_posts_where'), 10, 2 ); + add_filter( 'posts_where', array(&$this, 'content_in_posts_where'), 10, 2 ); } public function get_map() { @@ -333,4 +334,26 @@ class Items extends Repository { } return $where; } + + /** + * allow wp query filter post by array of content + * + * @param $where + * @param $wp_query + * @return string + */ + public function content_in_posts_where( $where, $wp_query ) { + global $wpdb; + if ( $post_content_in = $wp_query->get( 'post_content_in' ) ) { + if(is_array( $post_content_in ) && isset( $post_content_in['value']) ){ + $quotes = []; + foreach ($post_content_in['value'] as $title) { + $quotes[] = "'" . esc_sql( $wpdb->esc_like( $title ) ). "'"; + } + } + + $where .= ' '.$post_content_in['relation'].' ' . $wpdb->posts . '.post_content IN ( ' .implode(',', $quotes ) . ')'; + } + return $where; + } } \ No newline at end of file