Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Restrict searches to pages that have a particular Custom Field assigned
- This topic has 5 replies, 2 voices, and was last updated 10 years, 2 months ago by
Ernest Marcinko.
-
AuthorPosts
-
April 1, 2016 at 11:10 pm #8263
Ryan Logan
ParticipantHello. Sorry, I thought I had this figured out. I’d like to restrict searches to pages that have a particular Custom Field assigned. We’re trying to make it so that search only return data from Employee pages. If there is a better way to do it…I’m all ears.
Thank you,
RyanApril 4, 2016 at 9:11 am #8269Ernest Marcinko
KeymasterHi Ryan,
I suggest creating a “hidden” custom field filter, this is exactly what it’s designed for: http://i.imgur.com/UUgYzC1.png
More info in the documentation.
April 4, 2016 at 1:31 pm #8277Ryan Logan
ParticipantThank you. My fault – let me clarify further what I’m doing.
-I have an employee page named “Tom” and a non-employee page named “Home”.
-Tom has a custom field assigned named “Ajax Search”
-Home does not have a custom field assigned.
-Plugin: not showing/allownig users to change search settings criteria
-Plugin: configured to trigger on a minimum entry of 3 characters
-Plugin: on the General Options tab the only options I have turned on are Search in Pages, Search in Content and Search in Custom Fields > My custom field Ajax Search.Now, let’s say I do a search for “SEO”. This search shows results for Tom, but also shows results for Home.
We need Home and any other non-employee page to be ignored in search results.
Thanks again,
RyanApril 4, 2016 at 1:58 pm #8279Ernest Marcinko
KeymasterOk, I see what you need. You want to restrict the results where the “Ajax Search” custom field exists.
That’s a bit harder, but not impossible. I have a solution, which will work with the Regular engine.
Try putting this filtering function to the functions.php file in your active theme directory:[php]add_filter( ‘asp_cpt_query_add_where’, ‘asp_add_extra_where’, 1, 1 );
function asp_add_extra_where( $query ) {
global $wpdb;
// Change this to the custom field name
$custom_field = "custom_field";$query .= " AND EXISTS(SELECT 1 FROM $wpdb->postmeta as pm
WHERE pm.post_id = $wpdb->posts.ID AND pm.meta_key = ‘$custom_field’)";return $query;
}[/php]Change the $custom_field = … variable to the custom field key. This will add an extra paramter to the query to do a very fast check for custom field existence of the the given custom field for the result set.
April 4, 2016 at 2:10 pm #8280Ryan Logan
ParticipantRock-star support! It’s working beautifully. Thank you.
April 4, 2016 at 2:26 pm #8281Ernest Marcinko
KeymasterYou are welcome! I’m glad it’s working.
I will mark this topic as resolved now. Feel free to open a new one if you have other questions.
If you like the plugin, feel free to rate it on your codecanyon downloads page, it’s greatly appreciated.
-
AuthorPosts
- The topic ‘Restrict searches to pages that have a particular Custom Field assigned’ is closed to new replies.