• Resolved anthonybarro

    (@anthonybarro)


    Perhaps I’m not seeing the forest for the trees here, but how do you show most popular based on categories?

    I know how to exclude categories based on ID’s, but I want to show most popular in each particular parent- and/or subcategory, to help keep relevancy at its highest. Is this possible?

    I am outputting the content using the “manual” function you’ve shown, in functions.php and everything works a treat otherwise.

    • This topic was modified 8 years, 6 months ago by anthonybarro.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi there!

    The widget has a Category ID(s) field that you can use to set the IDs of the categories you wish to include / exclude from the listing (eg. 24,15,65,-3). Similarly, both the [wpp] shortcode and the wpp_get_mostpopular() template tag have the cat parameter that works exactly the same way.

    Thread Starter anthonybarro

    (@anthonybarro)

    Hey Hector, and thanks for the speedy reply.

    I realize I can include/exclude based on the ID’s in the widget settings, but isn’t there some sort of “get most popular from this category” parameter?

    E.g. if you’re on a “Blue Widgets” post, I’d want the sidebar populated with most popular from the “Widgets” category.

    I suppose I can make if/else statements for all my categories in the sidebar, but if there’s a handy “get most popular from the current category” I would want to use that.

    Plugin Author Hector Cabrera

    (@hcabrera)

    That’s not possible at the moment. There are plans to add category detection in a future release, no ETA though ??

    Thread Starter anthonybarro

    (@anthonybarro)

    Any way to do it manually through functions.php?

    Plugin Author Hector Cabrera

    (@hcabrera)

    Nope, as I said it needs to be built-in into the widget. Can’t be done via functions.php.

    Another user did something similar using the wpp_get_mostpopular() template tag instead. I don’t remember on which topic it was so I can’t show you what exactly he did, but here’s the idea:

    if ( is_single() ) {
    
        // Get post data
        global $wp_query;
        $queried_object = $wp_query->get_queried_object();
    
        // Get post categories
        $categories = get_the_category( $queried_object->ID );
    
        // WPP paremeters
        $args = array(
            'range' => 'monthly',
            'limit' => 10,
            'post_type' => 'post'
        );
    
        // Post has at least one category assigned,
        // so let's use its ID to list the most popular
        // from that category
        if ( !empty( $categories ) ) {
            $args['cat'] = $categories[0]->term_id;
        }
    
        // Display popular posts list
        wpp_get_mostpopular( $args );
    
    }
    Thread Starter anthonybarro

    (@anthonybarro)

    Thanks for the effort.

    I was wondering the same thing. I couldn’t find anything, so I tried to hack the plugin myself to accomplish that.

    I know updates are a pain if you hack a plugin, but to do my job for now (until the author of the plugin releases a new version supporting this feature) I had to do it this way.

    So for those of you who want wordpress popular posts to show popular posts from the categories the post you’re viewing belongs, here’s the code (anyone with better coding skills can fix it to suit WordPress coding standards):

    Open file wordpress-popular-posts.php and around line 1547 or before the code:

    // * categories
    			if ( !empty($instance['cat']) && $join_cats ) {

    Add this:

    $post = get_post();
    			if ( $post ) {
        			$categories = get_the_category( $post->ID );
    				$cat_count = count($categories);
    				$cats_ids = '';
    				foreach($categories as $category_pop) {
    					$cats_ids .= $category_pop->term_id.',';
    				}
        			$final_cats = rtrim($cats_ids,',');
        			$where .= " AND p.ID IN (
    						SELECT object_id
    						FROM {$wpdb->term_relationships} AS r
    							 JOIN {$wpdb->term_taxonomy} AS x ON x.term_taxonomy_id = r.term_taxonomy_id
    						WHERE x.taxonomy = 'category' AND x.term_id IN({$final_cats})
    						)";
    			}
    			

    I hope someone finds it useful, as I did.

    • This reply was modified 8 years, 6 months ago by CRE8.
    • This reply was modified 8 years, 6 months ago by CRE8.
    • This reply was modified 8 years, 6 months ago by CRE8.
    Thread Starter anthonybarro

    (@anthonybarro)

    That’s awesome dude! Thanks for sharing this. Will test it out shortly and report back.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Show most popular in the parent/child category’ is closed to new replies.