cabrerahector / wordpress-popular-posts

WordPress Popular Posts - A highly customizable WordPress widget that displays your most popular posts.
https://wordpress.org/plugins/wordpress-popular-posts/
GNU General Public License v2.0
279 stars 83 forks source link

Here is a way to get to the raw popular post data on an instance basis. #122

Closed pixeline closed 7 years ago

pixeline commented 7 years ago

Hello @cabrerahector and thank you very much for the work you've put into the plugin. This is my modest contribution, perhaps it will help other folks in similar situation.

In this project I'm busy on, we need the widget but also a place in the template where the output is hardcoded, with specific parameters and layout. I wanted to have access on the raw selection of posts returned by the plugin on an instance basis. (Using the filter as suggested in the documentation modifies the output for all instances).

I tweaked Phil Smart's example class to use the filter you provided. Here is the modified class, which returns an array of WP objects that I can then manipulate as i see fit. Handy.

In the template:

$popular_blog_posts = new Popular_Posts_Data(
      array(
            'range'     => 'monthly',
            'freshness' => 1,
            'orderby'   => 'views',
            'limit'     => 3,
            'post_type' => 'post,reportage'
      ) );

print_r( $popular_blog_posts->get_posts() );

Sample output:

Array
(
    [0] => stdClass Object
        (
            [id] => 29
            [title] => Sophie Rogers : les hommes de sa vie
            [date] => 2016-12-06 10:44:10
            [uid] => 4
            [pageviews] => 148
        )

    [1] => stdClass Object
        (
            [id] => 613
            [title] => RANIA OFFRE À JULIETTE UN CADEAU POUR SA FILLE
            [date] => 2016-12-12 11:46:18
            [uid] => 3
            [pageviews] => 50
        )

    [2] => stdClass Object
        (
            [id] => 12
            [title] => "Je veux m’occuper de moi, maintenant"
            [date] => 2016-11-30 19:24:05
            [uid] => 3
            [pageviews] => 39
        )
)
cabrerahector commented 7 years ago

Hi there!

I'm sure other users will take advantage of your work, so thanks for sharing! You might want to leave a comment on Phil's blog post to let him and his visitors know what you built.

Actually, I've been working on a native WPP class that does exactly this. You might want to check it out since it'll be the "stock" way of doing it in the (near) future

pixeline commented 7 years ago

Actually, I've been working on a native WPP class that does exactly this. You might want to check it out since it'll be the "stock" way of doing it in the (near) future

Ah, I was secretly hoping you'd say that :) It's definitely useful because, personally I am used to the WP_Query API and that would not require me to learn another one if I need to build a custom loop. I'll have a closer look in the coming days. Thanks!