Your code will check whether you are on a search page or not ( You also forgot to wrap your conditional in parentheses ).
In order to check if there is any result for your search, use have_posts()
:
if( have_posts() ) {
// There is a post
} else {
// No results
}
This works for global queries. If you wrote your own instance of WP_Query()
, you need to do as follows:
$my_query = new WP_Query($args);
if( $my_query->have_posts() ){
// There is a post
} else {
// No results
}
Related Posts:
- Rewriting search and pagination base
- Use wp_get_recent_posts with search term
- Count the number of times the search form template gets included on a page
- How to add seperate classes to no-search-result and found-search-result pages on wordrpess search – is_search()
- Search not showing all results
- Can anyone offer any help with this function?
- List post by title length
- Limit Search to Product Pages only unless I’m on Blogpage or Blogposts
- How can the searchform.php know if it’s used on a registered sidebar id ‘sidebar-1’ or ‘sidebar-2’?
- How can I include tags in wordpress search without a plugin
- Search box background on a different page template
- The search doesn’t work properly
- start_lvl Ignored in Custom walker_nav_menu
- Enforcing password complexity
- Customize default settings on new sites on multisite
- Check if a menu is empty?
- Dynamically change feature image in customiser
- Creating wordpress user registration form
- category not display in word press grammatically
- Setting up the child theme so as to enable right-to-left WordPress?
- Design view breaking on Pages
- Getting a fatal error while updating
- Hooking new functions to actions + passing parameters
- How do I get images (with a thumbnail preview) to show in search results?
- my function doesn’t return my post from today
- How to break down importing of feeds
- get_the_terms – only top level
- Have CSS Class Added with `is_page()` WP function
- Slide in Panel WordPress Post
- Calculations in functions.php [closed]
- Custom location for attached images
- Way to querry data (tags) from a wordpress database?
- WP Query with custom Shortcode
- Using fwrite() and “a” appends multiple times instead of once
- Where in PHP do I move title and meta (date) to bottom of each blog section?
- How to display posts by current user in a drop down
- How does WordPress insert Paragraphs in posts
- Linking text within textarea of custom meta box
- How do I add custom bulk actions to multiple custom post types?
- Making BuddyPress Activity Stream (Post Type) Featured Images Clickable [closed]
- what is the best practice to add new field to an api route
- Keyword checking in Gravity Forms
- how to refresh div on template part?
- Child page menu in sidebar
- Title Case WordPress Menu Items
- Alter required message using comment form api
- Widget back end radio button issue
- Why does this update_post_meta function not delete the custom field itself?
- Using data sent via AJAX in multiple functions on a WP plugin
- Detecting if parent page has gallery
- Utilize Function in Child Theme / Template File
- GET problem in WordPress
- Help with WordPress function inside a shortcode
- How to have different site identity logos on each page on Astra Theme [closed]
- WP All Import / Update stock quantity from multiple XML files
- Removing “Powered by” footer using child theme PHP [closed]
- Overide Variable in Child Theme
- How to style injected code in header section?
- Randomize Color Scheme Selection in Theme
- Include a file that has a function in it
- Execute Jquery when a specific page in my plugin is loading
- AJAX function not working [closed]
- SQL error with custom query
- Free search and custom taxonomy query not providing the same result
- Replace a single variable with add_filter
- Products listing check if meta checkbox is checked
- How to preload header logo image in WordPress? like what’s the code and where do I put it?
- Contain multiple page templates in one PHP custom template file in WordPress?
- How do I reopen the (Popup Maker) after entering the correct password for a password protected page?
- Display metabox galleries on specific page template in admin editor
- meta_query search names when they have middle initials
- display condition based on post term and status
- Only the first image uploads
- Custom plugin: how do I call a PHP file if settings option is set to true?
- removing the standard dashboard widgets in WP so I Can replace with custom
- Gravity Forms: How to add PHP function to confirmation conditional shortcode?
- Edit a function to take different actions based on user role
- How to put 2 php codes in functions.php without site crashing
- What is the correct way to search 3 custom fields only in WordPress?
- How to change order inside foreach using wp_enqueue_script?
- Change header on one page in WordPress
- How do I get the value of a current user’s custom field?
- Adding extra data to shortcode attributes and pass it to JS with wp_localize_script
- Allow Comments by Default for Multiple Post Types
- Cleaner ‘for each’ loop for registering scripts in functions.php
- How to get User Avatar Image with link to Author and User name in Html Title tag?
- WP HTTP API call response
- Fatal error when using array_diff() function inside of wp_update_nav_menu hook?
- How to get all users by user_login as a json_encode array?
- Private messaging – Getting and displaying the avatar/url of a message recipient
- Postmeta not saving
- Make the text field in search form optional
- Fix My Custom Function: Remove Automatic
- Hide the content before the more tag in the “single page”
- WP Debug enabled Undefined index error in a widget
- Particular meta tag – viewport – insertion when dealing with plugin
- how to loop through blog posts in php
- How to limit the number of posts in the wordpress loop in conjunction with the “download more” button?
- Need help with conditional logic for menus
- Custom Admin Menu Report for Specific User ID