Title: Get popular post IDs array
Last modified: August 22, 2016

---

# Get popular post IDs array

 *  Resolved [techjam](https://wordpress.org/support/users/techjam/)
 * (@techjam)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/get-popular-post-ids-array/)
 * Is there a function available to let you just get an array of the popular post
   IDs to use them in a custom loop?
 * [https://wordpress.org/plugins/wordpress-popular-posts/](https://wordpress.org/plugins/wordpress-popular-posts/)

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

 *  [andyaffleck](https://wordpress.org/support/users/andyaffleck/)
 * (@andyaffleck)
 * [11 years, 8 months ago](https://wordpress.org/support/topic/get-popular-post-ids-array/#post-5249729)
 * I was just coming here to search for this exact thing. So, I second this!
 *  [marcosmartins](https://wordpress.org/support/users/marcosmartins/)
 * (@marcosmartins)
 * [11 years, 8 months ago](https://wordpress.org/support/topic/get-popular-post-ids-array/#post-5249782)
 * I need too. I created this custom loop, but does not work.
 * _[ Moderator note: code fixed. Please wrap code in the backtick character or 
   [use the code button](http://codex.wordpress.org/Forum_Welcome#Posting_Code).]_
 *     ```
       <?php
        $querystr = "SELECT p.ID AS 'id' , p.post_title AS 'title' , p.post_date AS 'date' , p.post_author AS 'uid' , v.pageviews AS 'pageviews'
                     FROM (SELECT id , SUM(pageviews) AS pageviews , MAX(last_viewed) AS last_viewed
                     FROM wp_popularpostssummary WHERE last_viewed > DATE_SUB('2014-09-09 16:45:06' , INTERVAL 1 MONTH)
                     GROUP BY id ORDER BY pageviews DESC , last_viewed DESC) v LEFT JOIN wp_posts p ON v.id = p.ID
                     WHERE p.post_type IN('post' , 'page') AND p.ID NOT IN ( SELECT object_id
                     FROM wp_term_relationships AS r JOIN wp_term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
                     JOIN wp_terms AS t ON t.term_id = x.term_id WHERE x.taxonomy = 'cds-completos' AND t.term_id IN(17) ) AND p.post_password = ''
                     AND p.post_status = 'publish' LIMIT 14";
        $pageposts = $wpdb->get_results($querystr, OBJECT);
        ?>
   
       <?php if ($pageposts): ?>
        <?php global $post; ?>
        <?php foreach ($pageposts as $post): ?>
        <?php setup_postdata($post); ?>
   
       <?php the_title(); ?>
   
        <?php endforeach; ?>
        <?php else : ?>
           <h2>Not Found</h2>
        <?php endif; ?>
       ```
   
 *  [Bid13 Storage Auctions](https://wordpress.org/support/users/deweydb/)
 * (@deweydb)
 * [11 years, 8 months ago](https://wordpress.org/support/topic/get-popular-post-ids-array/#post-5249795)
 * Well that query is a whole lot of headfuck. Here’s something a lot simpler:
 *     ```
       $query = sprintf("SELECT postid from wp_popularpostsdata ORDER BY pageviews DESC LIMIT 4");
       $result = mysql_query($query);
       if ($result) {
       	while ($row = mysql_fetch_assoc($result)) {
       		echo "<pre>".print_r($row,1)."</pre>";
       	}
       	mysql_free_result($result);
       }
       ```
   
 *  [andyaffleck](https://wordpress.org/support/users/andyaffleck/)
 * (@andyaffleck)
 * [11 years, 8 months ago](https://wordpress.org/support/topic/get-popular-post-ids-array/#post-5249803)
 * I ended up doing the following:
    1. Build a query (similar to the two previous posts) that grabs the N most popular
       posts (in descending order).
    2. Loop through the results and build an array that is just those IDs in order
    3. Pass those ids into a WP_Query as follows:
    4.     ```
           $args = array(
           	'order' => 'DESC',
           	'orderby' => 'post__in',
           	'post__in' => $popularIDs, // my array of IDs
           );
           $posts_query = new WP_Query($args);
           ```
       
    5. Then I can do the usual WP Loop fun with the returned posts. The key is keeping
       them in order by using the proper orderby argument above (post__in).
 *  [validis](https://wordpress.org/support/users/validis/)
 * (@validis)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/get-popular-post-ids-array/#post-5249835)
 * Andy do you have an example of your code for part 1 and 2 please?
 *  [andyaffleck](https://wordpress.org/support/users/andyaffleck/)
 * (@andyaffleck)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/get-popular-post-ids-array/#post-5249836)
 * I do. The query-build part is from someone else (and right now my mind is blanking
   on whether it was a consultant we’d hired or something I’d found online). I stripped
   it down (it was much longer and complicated) and added in the bit at the end 
   where I build the array of the IDs in order (#2 in my earlier post). It includes
   the capability of time boxing the results though I currently only use it for 
   all posts on my sites.
 *     ```
       function get_popular_post_ids($args) {
   
       	global $wpdb;
       	$prefix = $wpdb->prefix . "popularposts";
       	$where = "";
       	$now = current_time('mysql');
   
       	/* Build the Query */
       	$fields = "p.ID AS 'id', p.post_title AS 'title', p.post_date AS 'date', p.post_author AS 'uid', v.pageviews AS 'pageviews'";
   
       	$from = "{$prefix}data v LEFT JOIN {$wpdb->posts} p ON v.postid = p.ID";
   
       	switch( $args['range'] ){
       		case "yesterday":
       			$where .= " AND p.post_date > DATE_SUB('{$now}', INTERVAL 1 DAY) ";
       			break;
   
       		case "daily":
       			$where .= " AND p.post_date > DATE_SUB('{$now}', INTERVAL 1 DAY) ";
       			break;
   
       		case "weekly":
       			$where .= " AND p.post_date > DATE_SUB('{$now}', INTERVAL 1 WEEK) ";
       			break;
   
       		case "monthly":
       			$where .= " AND p.post_date > DATE_SUB('{$now}', INTERVAL 1 MONTH) ";
       			break;
   
       		default:
       			$where .= "";
       			break;
       	}
   
       	$where .= " AND p.post_type = 'post'";
       	$where .= " AND p.post_password = '' AND p.post_status = 'publish'";	
   
       	$orderby = "ORDER BY pageviews DESC";
   
       	$query = "SELECT {$fields} FROM {$from} {$where} {$orderby};";
   
       	$result = $wpdb->get_results($query);
   
       	$counter = 0;
       	$myIDs = array();
   
       	foreach ($result as $aPost) {
       		$theID = $aPost->id;
   
       		if ( !$theID == "" ) {
       			$myIDs[$counter] = $theID;
       			$counter++;
       		}
       	}
   
       	return($myIDs);
       }
       ```
   
 * Hope this helps!

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

The topic ‘Get popular post IDs array’ 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
 * 5 participants
 * Last reply from: [andyaffleck](https://wordpress.org/support/users/andyaffleck/)
 * Last activity: [11 years, 6 months ago](https://wordpress.org/support/topic/get-popular-post-ids-array/#post-5249836)
 * Status: resolved