JoeHana
7/15/2013 - 12:05 AM

Exclude sold/rented listings from main loop, except from listing category pages and single view

Exclude sold/rented listings from main loop, except from listing category pages and single view

<?php
/**
 * Exclude sold/rented listings
 */
 
add_filter( 'pre_get_posts', 'wpsight_exclude_sold_rented' );
 
function wpsight_exclude_sold_rented( $query ) {

  if( is_admin() || is_tax( 'listing-category' ) || is_singular() )
    return;

  global $wpdb;
	
  $exclude_sold_rented = $wpdb->get_col( $wpdb->prepare( "
    SELECT DISTINCT post_id FROM {$wpdb->postmeta}
    WHERE meta_key = '%s'
    AND meta_value = '%s'
    ", '_price_sold_rented', '1' ) );

  if( ! empty( $exclude_sold_rented ) )
    $query->set( 'post__not_in', $exclude_sold_rented );

}