Trying to filter out specific posts that have certain meta_key from the main query... problem is it doesnt work with pagination which is my issue here.
Anyone knows how to solve this?
$paged = (end( explode( '/', $_SERVER['REQUEST_URI'] ) )) ? end( explode( '/', $_SERVER['REQUEST_URI'] ) ) : 1;
$posts_per_page = 10;
$offset = ($paged-1) * $posts_per_page;
function strip_codes_deals ($where = '', $ignore_posts) {
global $wpdb, $ignore_posts;
if (count($ignore_posts) > 0) {
$where .= ' AND ' . $wpdb->posts . '.ID NOT IN(' . implode (',', $ignore_posts) . ') ';
}
//echo $where;
return $where;
}
$wp_query = new WP_query( 'post_type=deals&orderby=date&order=desc&posts_per_page='.$posts_per_page.'&paged='.$paged.'&meta_key=codes-deal' );
// echo '<pre>';
// print_r($wp_query);
// echo '</pre>';
$ignore_posts = array();
if($wp_query) {
foreach ($wp_query->posts as $post) {
$codes_deal = get_post_meta($post->ID, "codes-deal", true);
if($codes_deal == 1) {
$ignore_posts[] = $post->ID;
}
}
echo '<pre>';
print_r($ignore_posts);
echo '</pre>';
}
add_filter( 'posts_where', 'strip_codes_deals' );
query_posts( $query_string . '&post_type=deals&orderby=date&order=desc&posts_per_page='.$posts_per_page.'&paged='.$paged );
remove_filter( 'posts_where', 'strip_codes_deals' );
This works for the first page, but all other pages are ignored. Any ideas how to fix this?