$sql_timeline = "SELECT __column__ FROM $wpdb->term_taxonomy INNER JOIN $wpdb->term_relationships ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id INNER JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->term_relationships.object_id WHERE $wpdb->term_taxonomy.taxonomy = 'post_tag' AND ($wpdb->posts.ID IN (".$following_user_format.") OR $wpdb->term_taxonomy.term_id IN (" . $following_tag_format . ")) ORDER BY $wpdb->posts.post_date DESC"; $prepare_args = array_merge($following_user_ids, $following_tag_ids); array_unshift($prepare_args, $sql_timeline); $_limit = $posts_per_page; $_offset = $posts_per_page * max($paged - 1 , 0); $sql_timeline = call_user_func_array(array($wpdb, 'prepare'), $prepare_args); $count_sql_timeline = str_replace('__column__', 'COUNT(*)', $sql_timeline); $select_sql_timeline = str_replace('__column__', "$wpdb->posts.ID", $sql_timeline)." LIMIT {$_limit} OFFSET {$_offset}"; $result_timeline = $wpdb->get_results($select_sql_timeline, ARRAY_N); $timeline_post_ids = array(); foreach ($result_timeline as $record) { $timeline_post_ids[] = $record[0]; } if(empty($timeline_post_ids)){ $timeline_post_ids = array(-1); } $args = array( 'posts_per_page' => $posts_per_page, 'post__in' => $timeline_post_ids, 'orderby' => 'post__in' ); global $mydb_found_timeline_posts; $mydb_found_timeline_posts = $wpdb->get_var($count_sql_timeline); function myprefix_adjust_offset_pagination(){ global $mydb_found_timeline_posts; return $mydb_found_timeline_posts; } add_filter('found_posts', 'myprefix_adjust_offset_pagination'); query_posts($args);