Last active
December 19, 2015 18:09
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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