0

Alright. I've worked with Wordpress for more than 10 years. I've never had a need to do this before and I don't think it's actually possible without some pretty custom dev work. So let me set this up. An item can be added to a collection. This item also has other taxonomies - topic, artist, and type. On the collection archive, I have select-able lists on the page that list the other taxonomies that the items can have.

My question is, when I click on a topic to filter the collection, what is the taxonomy query I need to get collections that have posts that have a selected topic? To make it more difficult, I'm working inside of a theme with Elementor and Crocoblock's JetEngine/JetSmartFilter and I've been asked to use those plugins (or a shortcode) to make it easier for the team to copy it to future sites.

Happy to answer questions and thank you in advance for any help!

1 Answer 1

0

Depending on use case scenario you might need to write some custom code or a tiny plugin to achieve this. If you want to filter the posts in admin dashboard post list then you can use following code snipped to jump start

<?php
if (is_admin()){
   //this hook will create a new filter on the admin area for the specified post type
   add_action( 'restrict_manage_posts', function(){
        global $wpdb, $table_prefix;

        $post_type = (isset($_GET['post_type'])) ? quote_smart($_GET['post_type'], true) : 'post';
 
        //only add filter to post type you want
        if ($post_type == 'YOUR_POST_TYPE_HERE'){
            $values = array();
            $query_years = $wpdb->get_results("SELECT year(post_date) as year from ".$table_prefix."posts
                    where post_type='".$post_type."'
                    group by year(post_date)
                    order by post_date");
            foreach ($query_years as &$data){
                $values[$data->year] = $data->year;
            }
            ?>
            <select name="admin_filter_year">
                <option value="">All years</option>
                <?php 
                $current_v = isset($_GET['admin_filter_year'])? $_GET['admin_filter_year'] : '';
                foreach ($values as $label => $value) {
                    printf(
                        '<option value="%s"%s>%s</option>',
                        $value,
                        $value == $current_v? ' selected="selected"':'',
                        $label
                    );
                }
                ?>
            </select>
            <?php
        }
    });

    //this hook will alter the main query according to the user's selection of the custom filter we created above:
    add_filter( 'parse_query', function($query){
        global $pagenow;
        $post_type = (isset($_GET['post_type'])) ? quote_smart($_GET['post_type'], true) : 'post';

        if ($post_type == 'YOUR_POST_TYPE_HERE' && $pagenow=='edit.php' && isset($_GET['admin_filter_year']) && !empty($_GET['admin_filter_year'])) {
            $query->query_vars['year'] = $_GET['admin_filter_year'];
        }
    });
}

If you want to do it in front end then my suggestion will be a custom query and ajax. For this scenario I'm for not be able to provide an example as it's a bit complex. As you have to use elementor in front end you can develop a widget for this too!

1
  • Thank you for your input - yes, I am trying to do it on the front-end. A widget or a shortcode will likely be the solution I end up with. Commented Feb 8, 2021 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.