Advanced Customization of "rep_listing" Query Filters

This guide will help you customize the query for the ‘rep_listing’ single page to filter results based on the number of bedrooms and specific categories.

1. Access Your Theme’s Functions File

  • To begin, log in to your WordPress admin dashboard.
  • Next, navigate to the “Appearance” menu and select “Theme Editor.”
  • In the right-hand panel, you’ll see a list of theme files. Find and click on the “functions.php” file associated with your currently active theme.

2. Insert the Provided Code

  • Once you’ve opened the “functions.php” file, scroll to the bottom or a convenient location for adding custom code.
  • This can also be done if you have installed code snippets plugin in your WordPress site like WPCode and Code Snippets to insert the code.
  • Copy and paste the following code snippet into the “functions.php” file:
// Override related elements to filter by bedrooms
add_filter('pre_get_posts', function( $query ){ 
	if( is_singular('rep_listing') && $query->get('post_type') == 'rep_listing' && ! $query->is_main_query() ){
		$post_id = get_the_ID();
		$beds = \Launchpad\lp__get_field_value( 'bedrooms', $post_id );
		$search_category = \Launchpad\lp__get_field_value( "search_category", $post_id );
		$category = \REP_Malcolm\rep__extract_search_categories([$search_category],'listing_category');
		$lp__meta_query = $query->get('lp__meta_query');
		if( ! $lp__meta_query ) $lp__meta_query = [];
		if( $beds && in_array('Residential', $category) ){
			$lp__meta_query[] = array(
				'key' => 'bedrooms',
				'value' => $beds,
				'compare' => '=',
				'type' => 'NUMERIC',
			);

			$query->set('lp__meta_query', $lp__meta_query);
		}
	}
});
This customization adapts the code to filter ‘rep_listing’ post type based on the ‘Residential’ category and the number of ‘bedrooms’.
You can apply similar customizations to change the filtering criteria as per your requirements. This flexibility allows you to tailor the code to various scenarios within your WordPress site.

3. Understanding the Code

  • This code is using a WordPress filter called pre_get_posts to modify the query that fetches posts.
    • We use pre_get_posts because almost all of our queries are aligned to wordpress WP_Query and also using custom query arguments.
  • It checks if the current page is displaying a single ‘rep_listing’ post and if it’s not the main query.
  • The code retrieves the number of bedrooms ($beds) and the search category ($search_category) associated with the current listing post.
  • It then extracts the ‘listing_category’ from the search category.
  • The code prepares or updates the lp__meta_query parameter to filter posts by the number of bedrooms, but only if the post falls into the ‘Residential’ category.

4. Save the Changes

After adding the code, click the “Update File” button to save your changes to the “functions.php” file.

5. Testing

To see the code in action, visit a single ‘rep_listing’ post on your WordPress site. The code will automatically filter related posts based on the number of bedrooms and the category.