wpsl_sql_placeholder_values

  1. Description
  2. Parameters
  3. Usage
  4. Changelog
  5. Source File
  6. Related

Description

This filter allows you to add values for the placeholders you add to the search query with the wpsl_sql filter.

The query runs through $wpdb->prepare(). Every %d or %s in the query needs a value in this array, in the same order as the placeholders appear in the query. So a new value has to be inserted at the position of its placeholder. Adding it to the end of the array doesn’t work.

These are the default values, in this order:

Position Value Included
0 The earth radius: 6371 for km, 3959 for miles. This is not the search radius. Always
1 The latitude of the start location Always
2 The longitude of the start location Always
3 The latitude again Always
4 and up One %value% pattern for each city, state or country restriction When the search is restricted
Next The search radius Radius search
Next The offset Radius search with a page offset
Last The max results Radius search

When Load locations on page load is on, the page load search ends with the Number of locations to show value, or with no extra value when that field is empty. The “show the nearest location” search has no values after the first four.

For example, a search from Rotterdam with a 500 km radius, 50 results and a country restriction gives:

[ 6371, 51.911093, 4.477297, 51.911093, '%Netherlands%', 500, 50 ]

With WPML, the max results and the Number of locations to show value are multiplied by the number of languages, and there is no separate offset value. The extra rows are removed after the query.

Parameters

$placeholder_values - (array) The placeholder values, in the order shown above.

Usage

This example hides locations that have a “Hide in search” checkbox checked. First add a Checkbox field with the name hide_in_search in Store Locator > Settings > Fields Manager. A checked box is saved as the post meta wpsl_hide_in_search with the value 1.

The wpsl_sql part adds two %s placeholders directly after WHERE posts.post_type = 'wpsl_stores'. That’s after the four distance placeholders and before the restriction placeholders, so array_splice inserts the two values at position 4.

add_filter( 'wpsl_sql_placeholder_values', 'custom_sql_placeholder_values' );
function custom_sql_placeholder_values( $placeholder_values ) {
/*
* The values for the two %s placeholders added in custom_hide_flagged_sql().
* They have to go right after the first 4 values ( earth radius, lat, lng, lat ),
* because that is where the placeholders are in the query.
*/
array_splice( $placeholder_values, 4, 0, [ 'wpsl_hide_in_search', '1' ] );
return $placeholder_values;
}
add_filter( 'wpsl_sql', 'custom_hide_flagged_sql' );
function custom_hide_flagged_sql( $sql ) {
global $wpdb;
// Skip locations where the "Hide in search" checkbox is checked.
$sql = str_replace(
"WHERE posts.post_type = 'wpsl_stores'",
"WHERE posts.post_type = 'wpsl_stores'
AND NOT EXISTS ( SELECT 1 FROM $wpdb->postmeta AS hide_flag WHERE hide_flag.post_id = posts.ID AND hide_flag.meta_key = %s AND hide_flag.meta_value = %s )",
$sql
);
return $sql;
}

Always change both filters together. If the number of values doesn’t match the number of placeholders, the values end up in the wrong place and the search returns no results.

When Load locations on page load is on, clear the cached results after you add the code. Go to Store Locator > Settings > Tools and click Clear cache next to Autoload cache.

Changelog

VersionDescription
3.0Moved to includes/frontend/search/class-search.php. The restriction values are now added after the first four values, before the search radius. An offset can come before the max results. The page load values are only used when Load locations on page load is on. With WPML the limit values are multiplied by the number of languages.
2.0Introduced.

Source File

This filter is located in /includes/frontend/search/class-search.php.

Placement

The code needs to be placed in the functions.php file inside your active theme folder.