The problem is that pre_get_posts
is called after you leave the page, so you must also test to see if the search was done from there and for that you can use wp_get_referer()
So (in theory, sorry I haven’t the time to test this) you could have
if ( !is_admin() && $query->is_search() && $query->is_main_query() && wp_get_referer() == "http://www.example.com/archive" ) {
Hope that helps
PS: Always add a !is_admin()
check to make double-sure you’re not effecting admin queries… belt & braces!
Related Posts:
- Display random categories on the front page (Finding and Editing Theme Functions)
- Order posts (across the whole site) by metadata date
- Sort posts by Date (DESC) and by Title (ASC)
- Filter the query ONLY for the search results page
- How to add classes to images based on their categories?
- current_cat_ancestor Alternatives
- Add body class of category parent
- Changing a function in function.php to a shortcode – for listing categories of only a certain post type
- Php string not working in WordPress Functions.php (trying to fetch 1st category for each blog that post appears in the sidebar)
- Add class to Categories Widget
- How to Acheive the custom woocommerce category template
- How to add a rel attribute to images that contains their categories?
- How to set child post categories to parent post categories when updating parent post?
- Assign category using custom field?
- How To Get WordPress Categories Link List?
- Changing post category from dropdown
- Manipulate Output of wp_list_something: select menu instead of li’s
- Make Categories a Dropdown Menu in Media Library
- Is it good practice to use wpdb->query() function?
- Programmatically Create Category and sub Category
- Attach parent category template to all subcategories
- Get the category name outside of the loop in category.php
- Is it possible to change any of the HTML/URL returned from the_category()
- example of build_query function?
- Custom HTML in specific category single page and its descendant categories
- Possible to display shortcode based on the category?
- Unset Category if other Category is unset during post transition
- Shortcode pagination not advancing
- Select pages by category
- Show Primary Category first when I display post categories
- How do I hide or remove ‘Category’ from wordpress breadcrumbs
- Ordering posts by publish date not working?
- Create a new query in function.php to filter blog posts
- Categories as main menu items and subcategories as nested lists
- Get category URL for current post
- Need help “sanitizing” a custom function that pulls category slug into body class
- allow only one post in specific category
- Connecting to a 2nd DB gives me no results when querying
- How to obtain the recent posts without their content in an efficient way?
- Setting a default text for excerpts of a particular category
- Running script based on Category
- Remove a category from a post when saving a new post
- How to ignore posts in pre_get_posts?
- How to fix get_the_category function returning incorrect slug?
- Return category name with & Ampersand doesnt work
- Access category within rss2_head hook?
- issue with if/elseif in_array inside foreach loop display only one post
- Randomizing wp_tag_cloud() in child theme
- add_filter to specific WooCommerce Category
- How to rewrite wordpress search to work on specific category
- Image loading function not working on archive.php template
- Exclude Category ID in function
- Problem with calling custom function in a foreach loop
- Specify multiple categories for custom post template – FATAL ERROR
- get_terms (or tax_query) for term of current post?
- “All posts” in the category widget
- Get Attachment Category Name
- shorthand syntax for custom fields
- WordPress Categories: Function using custom SQL to return array of specific category IDs
- Display ACF category image on archive and single template files
- Get slug of current category in functions.php
- How to divide Subcategories into pages of parent category wordpress
- Using One Function To Create Two Different Shortcodes
- Display a list of random terms from custom taxonomy with shortcode
- Change category display name function
- Display link to category over featured image
- Delete oldest wordpress post (SQL query)
- Selecting specific query for offset
- Remove the ‘category’ url for one category type
- Custom Function.PHP Code Not Working on One Site
- Adding new Category does not refresh the backoffice
- How do i tweak my wp Post title base on category of the post
- Why isn’t comment validation working?
- how to remove a tag in the_category function
- Menu and category highlighting for a single post
- i can’t use wordpress functions in ajax loaded php file
- Shortcode with pagination advancing multiple queries
- AJAX load more posts not using correct category and repeating the same few posts
- Graphic before title – Specific Category
- require_once() Causing categories and tag pages on dashboard to not refrsh
- different body classes for each category
- Function returning queried meta value based on current post ID
- How To Get WordPress Categories Last Update Date?
- How to get tags and categories?
- Sorting date results from a query
- How to Insert A List of Posts in A Category Written by the Author into the Author Archive
- Removing all Category pages with one exception
- Get category of post inside save_post hook
- Exclude a category ID from the following function
- Remove “Category:”, “Tag:”, “Author:” from the_archive_title
- Return only top-level navigation items from a menu using wp_get_nav_menu_items
- Enqueue styles after a plugin
- Function wont fire after custom post type is updated
- Do we have to use ftp to recover from a bad functions.php edit?
- reWrite wp-content url to point on my cdn
- can’t see categories in appearance-menu-categories
- get_comments not working properly after WordPress v4.4 update
- Including one page with WordPress functions into another
- Unhook the comment form in Buddypress
- How do i set global variable in a function in wordpress functions.php