Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query control to include specific categories and exclude some tags #7490

Closed
5 tasks done
silaslin opened this issue Mar 16, 2019 · 5 comments
Closed
5 tasks done

Query control to include specific categories and exclude some tags #7490

silaslin opened this issue Mar 16, 2019 · 5 comments
Labels
product/pro Indicates if the referenced component is part of the Elementor Pro plugin. request/feature Indicates a Request for a non-existing New Feature. status/merged Indicates when a Pull Request has been merged to a Release.
Milestone

Comments

@silaslin
Copy link

silaslin commented Mar 16, 2019

Prerequisites

  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • The issue still exists against the latest stable version of Elementor.

Description

Hi, I have written a widget to show tags list, but encountered a little problem in the query section.
First, I've to include categories , then save & reload, and then exclude unwanted tags.
Is there any solution to select tags immediately after the included categories changed?

Steps to reproduce

Isolating the problem

  • This bug happens with only Elementor plugin active (and Elementor Pro).
  • This bug happens with a default WordPress theme active.
  • I can reproduce this bug consistently using the steps above.

Environment

My code to show tags list
add_action( 'elementor/widgets/widgets_registered', function() {
	class tagslist extends \Elementor\Widget_Base {
		public function get_name() {
			return 'tags-list';
		}

		public function get_title() {
			return 'Tags List';
		}

		public function get_icon() {
			return 'fa fa-tags';
		}
		
		public function get_categories() {
			return [ 'basic' ];
		}
		
		protected function _register_controls() {
			$this->start_controls_section(
				'section_query',
				[
					'label' => __( 'Query', 'elementor-pro' ),
				]
			);

			$categories = get_terms( 'category' );

			$options = [];
			foreach ( $categories as $category ) {
				$options[ $category->term_id ] = $category->name;
			}

			$this->add_control(
				'include_categories',
				[
					'label' => __( 'Categories( Include )', 'elementor-pro' ),
					'type' => \Elementor\Controls_Manager::SELECT2,
					'options' => $options,
					'default' => [],
					'label_block' => true,
					'multiple' => true,
				]
			);

			$settings = $this->get_settings_for_display();

			$include_categories = $settings['include_categories'];
			$postids = get_objects_in_term( $include_categories, 'category' );
			$tags = wp_get_object_terms( (array)$postids, 'post_tag' );
			$options = [];
			foreach( $tags as $tag ){
		        $options[ $tag->term_id ] = $tag->name;
		    }

		    $this->add_control(	
				'exclude_tags',
				[
					'label' => __( 'Tags( Exclude )', 'elementor-pro' ),
					'type' => \Elementor\Controls_Manager::SELECT2,
					'options' => $options,
					'default' => [],
					'label_block' => true,
					'multiple' => true,
				]
			);

			$this->end_controls_section();

		}
			
		protected function render() {
			$settings = $this->get_settings_for_display();

			$include_categories = $settings['include_categories'];
			$exclude_tags = $settings['exclude_tags'];

			if ( empty( $include_categories ) ) {
				return;
			}

			$postids = get_objects_in_term( $include_categories, 'category' );
			if( !is_wp_error( $postids ) && !empty( $postids ) ){
		   		$tags = wp_get_object_terms( (array)$postids, 'post_tag' );
		        if( !is_wp_error( $tags ) && !empty( $tags ) ){
		        	$html .= '<ul class="tags-list">';
		        	foreach( $tags as $tag ){
		        		if ( !in_array( $tag->term_id, $exclude_tags ) ) {
		            		$html .= '<li><i class="fa fa-angle-right" aria-hidden="true"> </i><a href="' . get_term_link( $tag, 'post_tag' ) . '">' . ' ' . $tag->name . '</a></li>';
		            	}
		          	}
				  	$html .= '</ul>';
		        }
		    }
		    echo $html;
		}
	
	}

	\Elementor\Plugin::instance()->widgets_manager->register_widget_type( new tagslist() );
} );
@shilo-ey
Copy link
Contributor

Hi @silaslin

We are actively working on a similar feature.

Stay tuned!

@shilo-ey shilo-ey added request/feature Indicates a Request for a non-existing New Feature. product/pro Indicates if the referenced component is part of the Elementor Pro plugin. status/merged Indicates when a Pull Request has been merged to a Release. labels Mar 17, 2019
@shilo-ey shilo-ey added this to the Pro 2.5.0 milestone Mar 17, 2019
@Webtica
Copy link

Webtica commented Mar 17, 2019

+1 following

@silaslin
Copy link
Author

Hi @shilo-ey

It's cool and very nice feature of the Beta version.

I am trying to figure out the meaning of 'exclude' of Group_Control_Related::get_type().
This is my code below:

add_action( 'elementor/widgets/widgets_registered', function() {
	class tagslist extends \Elementor\Widget_Base {
		public function get_name() {
			return 'tags-list';
		}

		public function get_title() {
			return 'Tags List';
		}

		public function get_icon() {
			return 'fa fa-tags';
		}
		
		public function get_categories() {
			return [ 'basic' ];
		}
		
		protected function _register_controls() {
			$this->start_controls_section(
			'section_query',
			[
				'label' => __( 'Query', 'elementor-pro' ),
				'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
			]
			);

			$this->add_group_control(
				\ElementorPro\Modules\QueryControl\Controls\Group_Control_Related::get_type(),
				[
					'name' => 'tags',
					'presets' => [ 'include', 'exclude' ],
					'exclude' => [
						'posts_per_page', //use the one from Layout section
					],
				]
			);

			$this->end_controls_section();

		}
	

		protected function render() {

			$settings = $this->get_settings_for_display();

			$include_categories = $settings['tags_include_term_ids'];
			$exclude_tags = $settings['tags_exclude_term_ids'];

			if ( empty( $include_categories ) ) {
				return;
			}

			$postids = get_objects_in_term( $include_categories, 'category' );
			if( !is_wp_error( $postids ) && !empty( $postids ) ){
		   		$tags = wp_get_object_terms( (array)$postids, 'post_tag' );
		        if( !is_wp_error( $tags ) && !empty( $tags ) ){
		        	$html .= '<ul class="tags-list">';
		        	foreach( $tags as $tag ){
		        		if ( !in_array( $tag->term_id, $exclude_tags ) ) {
		            		$html .= '<li><i class="fa fa-angle-right" aria-hidden="true"> </i><a href="' . get_term_link( $tag, 'post_tag' ) . '">' . ' ' . $tag->name . '</a></li>';
		            	}
		          	}
				  	$html .= '</ul>';
		        }
		    }
		    echo $html;
		}

	}

	\Elementor\Plugin::instance()->widgets_manager->register_widget_type( new tagslist() );
} );
``

@shilo-ey
Copy link
Contributor

@silaslin

If I understand you correctly, the exclude options let you remove certain controls from the Group_Control_Related::get_type() controls set.

Thanks.

@silaslin
Copy link
Author

@silaslin

If I understand you correctly, the exclude options let you remove certain controls from the Group_Control_Related::get_type() controls set.

Thanks.

@shilo-ey

Thank you.
I got it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
product/pro Indicates if the referenced component is part of the Elementor Pro plugin. request/feature Indicates a Request for a non-existing New Feature. status/merged Indicates when a Pull Request has been merged to a Release.
Projects
None yet
Development

No branches or pull requests

4 participants