• Resolved GunnarGrotnes

    (@gunnargrotnes)


    Hello! I am new and hope to have found the correct place to ask!
    I have found this code snippet which should limit the search function to Post and Pages:

    <?php
    function searchfilter($query) {
         if ($query->is_search && !is_admin() ) {
            $query->set('post_type',array('post','page'));
        }
    return $query;
    }
    add_filter('pre_get_posts','searchfilter');
    ?>

    The author states:
    Notice the line that says
    $query->set('post_type',array('post','page'));
    You can filter the search results by changing the values in the array variable. Right now it is set to display posts and pages but you can modify it to display anything you want.
    How can I modify this code to search in a POD named «pod_a»?
    Obviously I should add the POD’s name in the array. But what would the correct syntax be?

    I also need to extend the code snippet to search in pod_a, pod_b or pod_c based on the user’s capabilities. Users having the role name user_a should only search in pod_a and so forth.
    What would be the php code to grab the user’s role name, test it and perform a search in the correct POD?
    Hope this is within the scope of your support service!
    Kind regards,
    Gunnar Grotnes

    • This topic was modified 5 years, 2 months ago by Jan Dembowski.
    • This topic was modified 5 years, 2 months ago by bcworkz. Reason: code fixed
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Setting a post_type array only applies to post types. Pods content types can be more than post types. You can set other query vars in a similar manner, but which to set depends on the Pods type you want to search for. If the Pods content type is an advanced type which has its own table, you cannot use standard WP_Query args to search, the SQL query itself would need to be altered.

    It’s not recommended to condition code on a user role itself, rather a specific capability should be used to allow more fine grained control over who can do what. You can check a current user’s capability with current_user_can(). That said, a user’s roles are available in the WP_User::roles property, which you can get through wp_get_current_user().

    Thread Starter GunnarGrotnes

    (@gunnargrotnes)

    Thank you for your quick answer!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Limit search function to a POD’ is closed to new replies.