This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

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

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #8263
    Ryan LoganRyan Logan
    Participant

    Hello. 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,
    Ryan

    #8269
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi 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.

    #8277
    Ryan LoganRyan Logan
    Participant

    Thank 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,
    Ryan

    #8279
    Ernest MarcinkoErnest 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:

    [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.

    #8280
    Ryan LoganRyan Logan
    Participant

    Rock-star support! It’s working beautifully. Thank you.

    #8281
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You 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.

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Restrict searches to pages that have a particular Custom Field assigned’ is closed to new replies.