Excluding Solds from main Property Listing

Easy Property Listings WordPress Real Estate Plugin Forums Basic Support Excluding Solds from main Property Listing

Tagged: ,

Viewing 5 posts - 1 through 5 (of 5 total)

These forums are closed to new replies / tickets. Please open a support ticket from our new Support page.

  • Author
    Posts
  • #20246
    honeycuttlee
    Spectator

    How would I go about excluding the “Sold” property from the default “Property” page listing? We’ve created a new “Sold Homes” listing using short codes, but I need to exclude the “Solds” from the main listing.

    Thanks.

    — Lee

    #20348
    Merv Barrett
    Keymaster

    Hi Lee

    You can use the pre_get_posts filter

    function my_property_filter($query) {
    	// Do nothing if is dashboard/admin or doing search
    	if ( is_admin() || epl_is_search() )
    		return;
    
    	// The query to only show 'current' listings
    	$meta_query = array(
    		array(
    			'key'=>'property_status',
    			'value'=>'current',
    			'compare'=>'==',
    		),
    	);
    
    	// Only show current listings on your main /property/ page
    	if ( $query->is_main_query() && is_post_type_archive( 'property' ) ) {
    		$query->set('meta_query',$meta_query);
            	return;
    	}
    
    	// Only show current listings on your main /rental/ page
    	if ( $query->is_main_query() && is_post_type_archive( 'rental' ) ) {
    		$query->set('meta_query',$meta_query);
            	return;
    	}
    }
    add_action( 'pre_get_posts', 'my_property_filter' , 20  );
    • This reply was modified 6 years, 9 months ago by Merv Barrett. Reason: updated function to add is_main_query()
    • This reply was modified 6 years, 8 months ago by Merv Barrett. Reason: added !is_admin() to prevent effecting the admin
    • This reply was modified 6 years, 8 months ago by Merv Barrett.
    #20554
    honeycuttlee
    Spectator

    Hey Merv,

    This filter works to remove the Sold listings. But it also removes the navigation bar from my Twenty-Twelve child theme. Got any clues why it’s doing this?

    — Lee

    #21101
    Merv Barrett
    Keymaster

    I’ve updated the function to include is_main_query() as per the pre_get_posts WordPress codex. That should correct your vanishing navigation menu.

    • This reply was modified 6 years, 9 months ago by Merv Barrett.
    #22175
    Merv Barrett
    Keymaster

    Updated function to prevent altering query in Dashboard/admin or if doing search.

    NOTE: Document added to Easy Property Listings Codex
    Excluding Solds from Property Loop

These forums are closed to new replies / tickets. Please open a support ticket from our new Support page.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘Basic Support’ is closed to new topics and replies.