{"id":4954,"date":"2025-04-21T15:12:29","date_gmt":"2025-04-21T14:12:29","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=4954"},"modified":"2025-05-10T09:30:26","modified_gmt":"2025-05-10T08:30:26","slug":"pre_get_posts","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/wordpress\/actions\/pre_get_posts","title":{"rendered":"pre_get_posts"},"content":{"rendered":"\n<p>This hook fires for pretty much everything WordPress does, every time anythign is displayed there is a search of some sort going on. You can use it to alter the results the search returns.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">The $Query object class<\/h4>\n\n\n\n<p>The $Query object that is passed to the hook shouldn&#8217;t be altered, but can be used to do lots of things &#8211; <a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/\" target=\"_blank\" rel=\"noreferrer noopener\">see here<\/a>.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Examples<\/h5>\n\n\n\n<h5 class=\"wp-block-heading\">Filtering search results to user doing a search using a search box<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>add_action('pre_get_posts', 'my_pre_get_posts');\nfunction my_pre_get_posts($Query)\n{\n\n  \/\/Do not change queries in the admin screens\n  if (is_admin())\n    return;\n  \n  \/\/Only modify the main search query on the front end\n  if (!$Query->is_main_query())\n    return;\n\n  if ($Query->is_search())\n  {\n    \/\/----------------------------------------\n    \/\/----- THIS IS A SEARCH BY THE USER -----\n    \/\/----------------------------------------\n\n    $UrlWithoutArguments = parse_url($_SERVER&#91;\"REQUEST_URI\"], PHP_URL_PATH);    \/\/Get page without any url arguments\n\n    if($UrlWithoutArguments == '\/my_search_results')\n    {\n      \/\/USER DOING A SEARCH ON PAGE \/my_search_results\n\n      \/\/Filter by author user_id's\n      $AllowedUserIds = &#91;2, 5, 10]; \/\/ Replace with your desired user IDs\n\n      if (count($local_vendor_user_ids) > 0)\n         $Query->set('author__in', $AllowedUserIds);\n      else\n         $Query->set('author__in', array(0));    \/\/If you return an empty array it will be ignored, to force no results use value 0 as it won't exist\n\n    }\n\n  } \/\/if ($Query->is_search())\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This hook fires for pretty much everything WordPress does, every time anythign is displayed there is a search of some sort going on. You can use it to alter the results the search returns. The $Query object class The $Query object that is passed to the hook shouldn&#8217;t be altered, but can be used to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[309,376],"tags":[],"class_list":["post-4954","post","type-post","status-publish","format-standard","hentry","category-actions","category-search"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4954","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=4954"}],"version-history":[{"count":4,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4954\/revisions"}],"predecessor-version":[{"id":4968,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4954\/revisions\/4968"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=4954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=4954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=4954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}