• Resolved Pascalvd

    (@pascalvd)


    Hi,

    First of all thanks for the great plugin. I’ve created a custom widget that will only show the most popular posts within the category of the active post:

    	$categories = get_the_category();  //get the category	
    	$active_category = array (
            'range' => 'last30days',
            'order_by' => 'views',
            'post_type' => 'post,exercises',
            'limit' => 5,
            'title_length' => 70,
            'cat' => $categories[0]->cat_ID,
    	'pid' => get_the_ID(),
            'thumbnail_width' => 200,
    	'thumbnail_height' => 200,
            'stats_views' => 0,
    	'stats_date' => 1,
            'post_html' => '<li style="margin-left: 0;"><div class="ba-pop-post"><div class="ba-pop-post-image">{thumb}</div><div class="ba-pop-post-info"><div class="ba-pop-post-title">{title}</div><div class="ba-pop-post-date">{date}</div></div></div></li>',
            );
            // output the query
            wpp_get_mostpopular($active_category);
    

    However, I’ve created a custom post type called ‘exercises’ and a custom taxonomy called ‘muscle groups’. I’ve already added the ‘exercises’ in the ‘post_type’ variable. I know I need to define ‘taxonomy’ and ‘term_id’ instead of ‘cat’, but how do I get the taxonomy and term_id dynamically, based on the active page? I’ve already tried ‘get_queried_object->taxonomy’ but that won’t work. Please help.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @pascalvd,

    Thanks for the kind words. Glad to know that you like the plugin.

    A couple of notes regarding your code:

    The way WPP’s taxonomy filter works -using your code as an example- is basically like this:

    “Hey WPP, please fetch posts and exercises that have this category and also these muscle group term IDs.”

    As you’re probably aware of, regular posts don’t have access to your muscle group taxonomy (unless you assigned this custom taxonomy to your posts when you registered it.) Likewise, your exercises post type doesn’t have access to the category taxonomy (unless you configured your CPT to be able to use categories when you registered it.)

    So, unless your posts can be tagged as muscle group X and your exercises can be given a post category, you won’t be able to list popular items filtered by category and muscle group like that. The plugin will not find any items that meet this criteria and in consequence you’ll get a “Sorry, no data so far” message.

    Again, all this is assuming that your posts don’t have access to the muscle group taxonomy and that your exercises can’t use regular categories.

    If what you actually want is to be able to list popular posts or popular exercises under the same category/taxonomy of the current post/exercise then this is how I would go about it (I removed some parameters for simplicity and clarity):

    $args = array (
        'post_type' => 'post, exercises',
        'pid' => get_queried_object_id()
    );
    
    // Get the post type of the current page
    $post_type = get_post_type(get_queried_object_id());
    
    // This is an exercise, fetch its muscle groups
    if ( 'exercises' == $post_type ) {
        $taxonomy = 'muscle_group';
    } // This is (likely) a post, fetch its categories
    else {
        $taxonomy = 'category'
    }
    
    $terms = get_the_terms(get_queried_object_id(), $taxonomy);
    
    // Current post/CPT does have terms associated to it,
    // let's get other popular items within the same term
    if ( $terms && ! is_wp_error($terms) ) {
        $args['taxonomy'] = $taxonomy;
        $args['term_id'] = $terms[0]->term_id;
    }
    
    // output the list
    wpp_get_mostpopular($args);
    Thread Starter Pascalvd

    (@pascalvd)

    Thanks a lot @hcabrera. I figured I could use get_post_type(). The code is working perfectly. ??

    Plugin Author Hector Cabrera

    (@hcabrera)

    No problem, glad I could help!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Filter by active term’ is closed to new replies.