Title: Retrieve posts as an array not HTML
Last modified: November 4, 2019

---

# Retrieve posts as an array not HTML

 *  Resolved [redwiredesign](https://wordpress.org/support/users/redwiredesign/)
 * (@redwiredesign)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/retrieve-posts-as-an-array-not-html/)
 * Hi,
 * It would be great if you could make a public function that just fetches the post
   objects and doesn’t render the HTML so we can have total control over the look
   of the results.
 * The main reason I want this is because there’s no way of checking the number 
   of popular posts before rendering the content. My theme will show different content
   depending upon if there are popular posts or not.
 * As it stands I will have to write my own function to check, before I render the
   function.
 * Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/retrieve-posts-as-an-array-not-html/#post-12094714)
 * Hi [@redwiredesign](https://wordpress.org/support/users/redwiredesign/),
 * There’s the [WordPressPopularPosts\Query class](https://github.com/cabrerahector/wordpress-popular-posts/blob/5.0.0/src/Query.php).
   Use that to retrieve post objects and build your posts list however you want/
   need.
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/retrieve-posts-as-an-array-not-html/#post-12094721)
 * Or you can use the [wpp_no_data filter hook](https://github.com/cabrerahector/wordpress-popular-posts/wiki/3.-Filters#wpp_no_data)
   to display alternative content when no popular posts were found.
    -  This reply was modified 6 years, 7 months ago by [Hector Cabrera](https://wordpress.org/support/users/hcabrera/).
      Reason: Fixed typo
 *  Thread Starter [redwiredesign](https://wordpress.org/support/users/redwiredesign/)
 * (@redwiredesign)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/retrieve-posts-as-an-array-not-html/#post-12095333)
 * Thank you I’ll try the class.
 * Unfortunately the wpp_no_data filter isn’t useful in my case. The PHP still has
   no idea if there are any popular posts or not.
 * It would be useful perhaps to add a param to return the string of HTML instead
   of echoing it from within the function. At least then we can determine if there
   are popular posts or not in the code.
 * Thanks.
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/retrieve-posts-as-an-array-not-html/#post-12095347)
 * > Unfortunately the wpp_no_data filter isn’t useful in my case. The PHP still
   > has no idea if there are any popular posts or not.
   > It would be useful perhaps to add a param to return the string of HTML instead
   > of echoing it from within the function. At least then we can determine if there
   > are popular posts or not in the code.
 * No need, WPP does that check for you automatically and if no posts are found 
   then that filter hook is called (see [related code here](https://github.com/cabrerahector/wordpress-popular-posts/blob/5.0.1/src/Output.php#L235-L238)).
 *  Thread Starter [redwiredesign](https://wordpress.org/support/users/redwiredesign/)
 * (@redwiredesign)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/retrieve-posts-as-an-array-not-html/#post-12095391)
 * I appreciate how the filter works, however the no data filter doesn’t affect 
   the header. The function still prints out the header even if there are no posts.
 * I’m sure you can imagine how embarrassing it is for a website to have a heading
   saying “Popular posts” and then there being none. It’s far better to show nothing
   until there is data.
 * I shall use the class so I can achieve what I’m trying to do. Thanks.
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/retrieve-posts-as-an-array-not-html/#post-12095411)
 * Ah, that is true. Maybe I should improve that on future releases. Thanks for 
   the reminder!
 * Anyways, I’m marking this topic as **resolved**. If you need help with anything
   else don’t hesitate to ask.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Retrieve posts as an array not HTML’ is closed to new replies.

 * ![](https://ps.w.org/wordpress-popular-posts/assets/icon-256x256.png?rev=1232659)
 * [WP Popular Posts](https://wordpress.org/plugins/wordpress-popular-posts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wordpress-popular-posts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wordpress-popular-posts/)
 * [Active Topics](https://wordpress.org/support/plugin/wordpress-popular-posts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wordpress-popular-posts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wordpress-popular-posts/reviews/)

 * 6 replies
 * 2 participants
 * Last reply from: [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * Last activity: [6 years, 7 months ago](https://wordpress.org/support/topic/retrieve-posts-as-an-array-not-html/#post-12095411)
 * Status: resolved