Reply To: Restrict searches to pages that have a particular Custom Field assigned

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Restrict searches to pages that have a particular Custom Field assigned Reply To: Restrict searches to pages that have a particular Custom Field assigned

#8279

Ernest Marcinko
Keymaster

Ok, 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:

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; 
}

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.

Best,
Ernest Marcinko

If you like my products, don't forget to rate them on codecanyon :)