diff --git a/src/classes/repositories/class-tainacan-items.php b/src/classes/repositories/class-tainacan-items.php index 97de52af4..967048945 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -347,6 +347,10 @@ class Items extends Repository { $args['orderby'] = $new_order; } + if ( defined('TAINACAN_ENABLE_RELATIONSHIP_METAQUERY') && true === TAINACAN_ENABLE_RELATIONSHIP_METAQUERY ) { + $args = $this->parse_relationship_metaquery($args); + } + $args = apply_filters( 'tainacan_fetch_args', $args, 'items' ); $should_filter = is_user_logged_in() && ! isset($args['post_status']) && sizeof($cpt) > 1; @@ -698,4 +702,40 @@ class Items extends Repository { return $response; } + private function parse_relationship_metaquery ($args) { + if( isset($args['meta_query']) ) { + $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); + foreach($args['meta_query'] as $idx => $meta) { + $meta_id = $meta['key']; + $metadata = $Tainacan_Metadata->fetch($meta_id); + if($metadata->get_metadata_type() === 'Tainacan\\Metadata_Types\\Relationship') + { + $options = $metadata->get_metadata_type_options(); + if( isset($options) && isset($options['search']) ) { + $this->relationsip_metaquery = array( + 'meta_id' => $meta_id, + 'search_meta_id' => $options['search'], + 'search_meta_value' => $args['meta_query'][$idx]['value'] + ); + $args['meta_query'][$idx]['compare'] = '!='; + $args['meta_query'][$idx]['value'] = ''; + add_filter( 'posts_where' , array($this, 'posts_where_relationship_metaquery'), 10, 1 ); + return $args; + } + } + } + } + return $args; + } + + function posts_where_relationship_metaquery( $where ) { + $meta_id = $this->relationsip_metaquery['meta_id']; + $search_meta_id = $this->relationsip_metaquery['search_meta_id']; + $search_meta_value = $this->relationsip_metaquery['search_meta_value']; + $SQL_related_item = " SELECT DISTINCT post_id FROM wp_postmeta WHERE meta_key=$search_meta_id AND meta_value LIKE '%$search_meta_value%'"; + $where .= " AND (wp_postmeta.meta_key = '$meta_id' AND wp_postmeta.meta_value IN ( $SQL_related_item ) ) "; + remove_filter( 'posts_where', array($this, 'posts_where_relationship_metaquery') ); + return $where; + } + } diff --git a/src/views/admin/components/metadata-types/relationship/FormRelationship.vue b/src/views/admin/components/metadata-types/relationship/FormRelationship.vue index 5808352bf..cd0c19853 100644 --- a/src/views/admin/components/metadata-types/relationship/FormRelationship.vue +++ b/src/views/admin/components/metadata-types/relationship/FormRelationship.vue @@ -131,7 +131,8 @@ modelSearch:'', collectionType: '', collectionMessage: '', - displayRelatedItemMetadata: [] + displayRelatedItemMetadata: [], + isMetaqueryRelationshipEnabled: tainacan_plugin && tainacan_plugin.tainacan_enable_relationship_metaquery == true ? tainacan_plugin.tainacan_enable_relationship_metaquery : false } }, computed: { @@ -205,7 +206,7 @@ this.metadata = []; for (let metadatum of metadata) { - if (metadatum.metadata_type_object.component !== 'tainacan-relationship' && metadatum.metadata_type_object.component !== 'tainacan-compound') { + if ( (metadatum.metadata_type_object.component !== 'tainacan-relationship' || this.isMetaqueryRelationshipEnabled) && metadatum.metadata_type_object.component !== 'tainacan-compound' ) { this.metadata.push( metadatum ); this.hasMetadata = true; this.checkMetadata(); diff --git a/src/views/class-tainacan-admin.php b/src/views/class-tainacan-admin.php index 3ea2d2263..2e758b051 100644 --- a/src/views/class-tainacan-admin.php +++ b/src/views/class-tainacan-admin.php @@ -307,7 +307,8 @@ class Admin { 'repository_name' => get_bloginfo('name'), 'api_max_items_per_page' => $TAINACAN_API_MAX_ITEMS_PER_PAGE, 'wp_elasticpress' => \Tainacan\Elastic_Press::get_instance()->is_active(), - 'item_submission_captcha_site_key' => get_option("tnc_option_recaptch_site_key") + 'item_submission_captcha_site_key' => get_option("tnc_option_recaptch_site_key"), + 'tainacan_enable_relationship_metaquery' => ( defined('TAINACAN_ENABLE_RELATIONSHIP_METAQUERY') && true === TAINACAN_ENABLE_RELATIONSHIP_METAQUERY ) ]; $maps = [