get popular post id as an array with no HTML
-
Hector,
Is there a query to return the most popular post ids as a text list or an array. I would like to use them in an elementor query, to display popular post in a format to match the other posts on the site
thanks
John Penney
-
Hi John,
You can use the WordPressPopularPosts\Query class to fetch an array of popular posts objects. Loop said array to obtain the IDs of each item.
If you have any further questions don’t hesitate to ask.
Héctor,
Thanks for your response. My technical skills are not very deep. Can you give me any insight into how I can install the class in my child them and loop the array?
I appreciate any help you could provide.
Thanks,
John PenneyAlright, no problem John.
Untested but it should work. This function will return an array of posts IDs, or an empty array if no popular posts were found:
/** * Retrieves popular posts IDs. * * @param string $range * @param int $limit * @return array function get_popular_posts_ids($range = 'last7days', $limit = 10) { $post_IDs = array(); $query = new \WordPressPopularPosts\Query([ 'range' => $range, 'limit' => $limit ]); $popular_posts = $query->get_posts(); // Popular posts found, get their IDs if ( is_array($popular_posts) && ! empty($popular_posts) ) { foreach($popular_posts as $popular_post) { $post_IDs[] = $popular_post->id; } } return $post_IDs; }
Usage examples:
// Gets the 10 most viewed posts from the last 7 days $post_ids = get_popular_posts_ids(); // Get the 10 most viewed posts from the last 24 hours $post_ids = get_popular_posts_ids('last24hours'); // Gets the 3 most viewed posts from the last 30 days $post_ids = get_popular_posts_ids('last30days', 3);
Give it a try and let me know how it goes, alright?
Hector,
I want to thank you for your response. This works well. IF you want to see it, it is in the section: This months most popular, in the center of the page
https://www.passblue.com/but I believe i found an issue that I want to ask you about. When I click on troubleshooting mode, this I get a fatal error in the code. I have to comment out the code in functions to get the site back. I would guess that the code is looking for the plugin, which can not be found. Is there a way to solve that problem?
Thanks,
JohnHi @jspenney,
What is this “troubleshooting mode” exactly? And what fatal error are you getting?
It is under tools > site health> troubleshooing mode
I am not quite sure but I think it turns off all your plugins and you can turn them on individually to test problems.the error is
Fatal error: Uncaught Error: Class ‘WordPressPopularPosts\Query’ not found in /home/dulcie/public_html/wp-content/themes/generatepress_child/functions.php:43 Stack trace: #0 /home/dulcie/public_html/wp-content/themes/generatepress_child/functions.php(62): get_popular_posts_ids(‘last30days’, 5) #1 /home/dulcie/public_html/wp-settings.php(514): include(‘/home/dulcie/pu…’) #2 /home/dulcie/public_html/wp-config.php(128): require_once(‘/home/dulcie/pu…’) #3 /home/dulcie/public_html/wp-load.php(37): require_once(‘/home/dulcie/pu…’) #4 /home/dulcie/public_html/wp-admin/admin.php(34): require_once(‘/home/dulcie/pu…’) #5 /home/dulcie/public_html/wp-admin/index.php(10): require_once(‘/home/dulcie/pu…’) #6 {main} thrown in /home/dulcie/public_html/wp-content/themes/generatepress_child/functions.php on line 43
it starts at line 39
this is line 43
$query = new \WordPressPopularPosts\Query([
————————————–
function get_popular_posts_ids($range = ‘last7days’, $limit = 10, $pid = ‘16817,6620’)
{
$post_IDs = array();$query = new \WordPressPopularPosts\Query([
‘range’ => $range,
‘limit’ => $limit,
‘pid’ => $pid
]);
$popular_posts = $query->get_posts();// Popular posts found, get their IDs
if ( is_array($popular_posts) && ! empty($popular_posts) ) {
foreach($popular_posts as $popular_post) {
$post_IDs[] = $popular_post->id;
}
}return $post_IDs;
}It is under tools > site health> troubleshooing mode
I am not quite sure but I think it turns off all your plugins and you can turn them on individually to test problems.Ah, I had no idea such a thing existed (being a dev myself I don’t have a need for a troubleshooting mode haha) but thanks for updating me on the matter. Might find it useful someday.
Fatal error: Uncaught Error: Class ‘WordPressPopularPosts\Query’ not found in (…)
Yes, your theme is complaining about a PHP class that doesn’t exist. Said class is included by WPP when the plugin is active and since the troubleshooting mode disables all plugins, well, the class is never loaded by your site. Hence the error message you’re seeing.
Change the above function to this to prevent it from triggering that error message:
/** * Retrieves popular posts IDs. * * @param string $range * @param int $limit * @return array function get_popular_posts_ids($range = 'last7days', $limit = 10) { $post_IDs = array(); if ( is_plugin_active('wordpress-popular-posts/wordpress-popular-posts.php') ) { $query = new \WordPressPopularPosts\Query([ 'range' => $range, 'limit' => $limit ]); $popular_posts = $query->get_posts(); // Popular posts found, get their IDs if ( is_array($popular_posts) && ! empty($popular_posts) ) { foreach($popular_posts as $popular_post) { $post_IDs[] = $popular_post->id; } } } return $post_IDs; }
-
This reply was modified 4 years, 7 months ago by
James Huff.
-
This reply was modified 4 years, 7 months ago by
Hector Cabrera. Reason: Improved wording for clarity
Hector
I still have a problem in troubleshooting mode. When I turn it on, i get this errorFatal error: Uncaught Error: Call to undefined function is_plugin_active() in /home/dulcie/public_html/wp-content/themes/generatepress_child/functions.php:43 Stack trace: #0 /home/dulcie/public_html/wp-content/themes/generatepress_child/functions.php(65): get_popular_posts_ids(‘last30days’, 5) #1 /home/dulcie/public_html/wp-settings.php(514): include(‘/home/dulcie/pu…’) #2 /home/dulcie/public_html/wp-config.php(128): require_once(‘/home/dulcie/pu…’) #3 /home/dulcie/public_html/wp-load.php(37): require_once(‘/home/dulcie/pu…’) #4 /home/dulcie/public_html/wp-admin/admin.php(34): require_once(‘/home/dulcie/pu…’) #5 /home/dulcie/public_html/wp-admin/index.php(10): require_once(‘/home/dulcie/pu…’) #6 {main} thrown in /home/dulcie/public_html/wp-content/themes/generatepress_child/functions.php on line 43
line 43 is
if ( is_plugin_active(‘wordpress-popular-posts/wordpress-popular-posts.php’) ) {If I disable the WP Popular posts plugin, all is fine.
I am working on this. When I deactivated all pluging, I got the same error so it may be a plugin conflict
there is some plugin conflict but it is hard to find. If I disable all my plugins at once, I get the error on line 43. If I start disabling plugins randomly, then it crashes when most of the plugins have been disabled, but it crashes on a different plugin every time. Unusual problem. Any ideas?
The is_plugin_active() function is a core WordPress function. If you’re getting errors about it being undefined then the cause of your problem lies somewhere else. Maybe your WordPress setup got corrupted somehow?
Hector,
Yes, something is a little funny. I appreciate that you took the time to check into this for me.
-John -
This reply was modified 4 years, 7 months ago by
- The topic ‘get popular post id as an array with no HTML’ is closed to new replies.