Title: get_posts() JOIN another table ?
Last modified: August 20, 2016

---

# get_posts() JOIN another table ?

 *  [John Bui](https://wordpress.org/support/users/phuclorddn/)
 * (@phuclorddn)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/get_posts-join-another-table/)
 * My site use WooTheme and I want to display popular posts on Popular tab within
   WooTabs Widget as Most Read Posts in a week, not as Most Comment Count by default.
 * Here’s the default code of WooTabs’s Popular Tab:
 *     ```
       if (!function_exists('woo_tabs_popular')) {
       	function woo_tabs_popular( $posts = 5, $size = 35 ) {
       		global $post;
       		$popular = get_posts('caller_get_posts=1&orderby=comment_count&showposts='.$posts);
       		foreach($popular as $post) :
       			setup_postdata($post);
       	?>
       	<li>
       		<?php if ($size <> 0) woo_image('height='.$size.'&width='.$size.'&class=thumbnail&single=true'); ?>
       		<a title="<?php the_title(); ?>" href="<?php the_permalink() ?>"><?php the_title(); ?></a>
       		<span class="meta"><?php the_time( get_option( 'date_format' ) ); ?></span>
       		<div class="fix"></div>
       	</li>
       	<?php endforeach;
       	}
       }
       ```
   
 * I try to find a plugin to record post hits and I installed **“Most Read in XX
   Days”**.
 * This Plugin create a table named “most_read_hits”:
    (ID, post_ID, hits) and any
   post read count within a number of days will be recorded in to “hits” field.
 * Now I want to use the “hits” field in “most_read_hits” to get and order some 
   posts get from get_post() (Edit the code of “WooTabs Popular Posts” above.
 * I tried to edit
    `$popular = get_posts('caller_get_posts=1&orderby=comment_count&
   showposts='.$posts);` to `$popular = get_posts('caller_get_posts=1&join="JOIN
   $wpdb->most_read_hits ON $wpdb->posts.ID = $wpdb->most_read_hits.post_ID"&orderby
   =$wpdb->most_read_hits.hits&showposts='.$posts);` but it only lead to displaying
   latest posts.
 * Anyone help me solve this?
    Thank you!

The topic ‘get_posts() JOIN another table ?’ is closed to new replies.

## Tags

 * [get_posts](https://wordpress.org/support/topic-tag/get_posts/)
 * [join](https://wordpress.org/support/topic-tag/join/)
 * [popular](https://wordpress.org/support/topic-tag/popular/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 0 replies
 * 1 participant
 * Last reply from: [John Bui](https://wordpress.org/support/users/phuclorddn/)
 * Last activity: [14 years, 5 months ago](https://wordpress.org/support/topic/get_posts-join-another-table/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
