Skip to content

Instantly share code, notes, and snippets.

@JoeHana
Last active December 19, 2015 18:09
Show Gist options
  • Save JoeHana/5996691 to your computer and use it in GitHub Desktop.
Save JoeHana/5996691 to your computer and use it in GitHub Desktop.
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 );
}
@JoeHana
Copy link
Author

JoeHana commented Jul 15, 2013

Here is a version where the single listing pages works if sold/rented listings should be visible through a dedicated listing category page: https://gist.github.com/JoeHana/5996691

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment