WordPress.org

Forums

WordPress Popular Posts
Output as array of post objects. (7 posts)

  1. roberttt1005
    Member
    Posted 1 year ago #

    I modified your class to use in my functions file to get the popular posts as an array of objects so I can display them in different ways rather than sticking to a template or whatever. I'm not sure if I over-killed it but it works like a charm for me.

    Gist

    http://wordpress.org/plugins/wordpress-popular-posts/

  2. Hector Cabrera
    Member
    Plugin Author

    Posted 1 year ago #

    Hi Robert, sorry for the late reply.

    It seems the gist is missing or that the URL is no longer valid. Anyways, you might be interested in checking my Github repo, I've added a hook (wpp_custom_html) which you can use to get the raw query result in order to build your own HTML list.

  3. sood_sumit19
    Member
    Posted 1 year ago #

    Where is the wpp_custom_html and how can we use it. I have same sort of problem and need the result in an array

  4. Hector Cabrera
    Member
    Plugin Author

    Posted 1 year ago #

    Be aware that this hook I mentioned above is not available on the current official release (version 2.3.7 at the time of writing this). You can only use it on the development version on Github.

    This is how you would hook to the raw query result:

    /*
     * @param string $html: original HTML output.
     * @param array $popular_posts_array : array of objects containing
     * the popular posts information.
     */
    function my_custom_html( $html, $popular_posts_array ){
    	// Loop $popular_posts_array and build your custom HTML,
    	// and return it when you're done.
    }
    add_filter( 'wpp_custom_html', 'my_custom_html', 10, 2 );

    Add that piece of code to your theme's functions.php file.

    Keep in mind that the code on Github is still highly experimental and it may be changed / removed without prior notice.

  5. pakus
    Member
    Posted 1 year ago #

    Hi,

    I'd very interested to be able to call a function in order to get an array of popular post. It would be similar to class private get_popular_posts().

    I'm trying to use this data with a 3rd party widget.

    Thanks in advance.

  6. Hector Cabrera
    Member
    Plugin Author

    Posted 1 year ago #

    Hi pakus,

    This is how I would do it:

    1. Hook into the plugin using the wpp_custom_html filter as explained above to retrieve the array of posts.
    2. Then, use wpp_get_mostpopular() to echo the list.

    All this, of course, once version 3.0 is officially available.

  7. ajayver
    Member
    Posted 1 year ago #

    <?php 
    
    if (function_exists('wpp_get_mostpopular')) {
    
    	add_filter( 'wpp_html', 'wpp_get_array', 10, 2);
    
    	function wpp_get_array($content, $posts_data) {
    
    		return $posts_data;
    
    	}
    
    	$popular_posts_object = new WordPressPopularPosts;
    
    	$instance = array(
    		'range' => 'monthly',
    	);
    
    	$popular_posts = $popular_posts_object->get_popular_posts( $instance );
    
    	var_dump($popular_posts);
    
    }

    This code gets an array of popular posts to $popular_posts variable.
    Works with Popular Posts plugin since 2.3.6 version.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WordPress Popular Posts
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags