How to filter Post Type and category in elementor widget

Question

Hope you are doing well.

I want to create a custom widget for WordPress Elementor Plugin to filter Posts by Post Type and Category.

I used this code to filter Post Type and Categories

function get_post_types() {
    $post_types = get_post_types( ['public'   => true ], 'objects' );
    foreach ($post_types as $post_type) {
        $posts[$post_type->name] = $post_type->labels->singular_name;
    }
    return $posts;
}


function get_post_cat(){
    $terms = get_terms( array(
        'taxonomy' => 'category',
        'hide_empty' => true,
    ));

    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
        foreach ( $terms as $term ) {
            $options[ $term->term_id ] = $term->name;
        }
        return $options;
    }
}

And this Elementror widget Controllers :

      $this->add_control(
        'aelso_posts_select_post_type',
        [
            'label' => __( 'Select Post Type', 'plugin-domain' ),
            'type' => \Elementor\Controls_Manager::SELECT2,
            'multiple' => true,
            'options' => $this->get_post_types(),
            'default' => 'post',
        ]
    );


    $this->add_control(
        'aelso_posts__category',
        [
            'label' => __( 'Select Category', 'plugin-domain' ),
            'type' => \Elementor\Controls_Manager::SELECT2,
            'multiple' => true,
            'options' => $this->get_post_cat(),
        ]
    );

Unfortunately the Category filter not working correctly. When I am selecting the event post type and than I want to select the Category, It show me the other post type (WordPress Posts) category. and I cant filter posts based on category of each post type.
Please help me to fix this issue.

Thanks and many regards

0
Mohammad Ali Khodadadi 2 years 2022-03-28T06:32:31-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse