Assuming you are using the standard WordPress search, you can get the searched number with get_search_query
So this code will create a new draft post if no results were found for the search:
$match = get_page_by_title( sanitize_title( get_search_query() ), OBJECT, ['post_type' => 'post'] );
if ( empty( $match ) ) {
wp_insert_post(
['post_title' => sanitize_title( get_search_query() ) ]
);
}
The above snippet would be added to the search.php
template in your theme.
Related Posts:
- Is it possible to get the specific content on the search page?
- How to get post content by calling ajax?
- How to show all posts of the category in wordpress?
- Get the ID of the latest post
- How to add a button to custom post type’s posts-page
- Limit the post for differents custom post type in the same wp_query
- How to hook get_terms() to only show count of posts that have custom meta
- Loading post content in FancyBox
- Query post types with multiple keys
- ascending order custom post type
- Get all posts WHERE custom_field is LIKE value
- Quickest way to get last or oldest post date – WP Query
- Display only one post each WEEK
- List children on child post
- Get latest 3 posts from multiple CPT in one query
- Add post location with mile radius allowing search
- Homepage’s content is dependent on the custom field values (set automatically), how do I get homepage to update without manually updating page?
- Limit the post for differents custom post type in the same wp_query
- BBPress Search results in WordPress search
- wordpress remove views from action links in a custom post
- Custom Empty Results page for my Custom Post Type
- What’s an efficient way to change the post status of many posts at once?
- find custom post type post by searching its custom field with my string
- Add to ‘action’ within post.php to allow more actions when editing a Custom Post Type in a plugin
- Best way to create a search for custom post type by custom field values
- List all posts in taxonomy term
- Building a Data Intensive Website with WordPress
- custom post type and user post count shortcode
- Correct way adding External classes to Custom Post type and output their methods to template
- WP_List_Table Inside Metabox Not Working on Clicking Search Button
- minimize wp_query call to database
- Widget area for individual posts (custom post type)?
- Use WP pagination functions on a custom page template
- call a function when insert and update a custom post type
- update a posts of other custom post type
- How to set class to “current-page” on the currently viewed single post within a CPT
- How to get Last post Id of custom post type in wordpress
- query_post causes the posts to be loaded twice on load more posts
- Search page for custom post type
- Custom post type blog pagination conflict
- Query Posts From Multiple Post Types
- WP Build-In Post selector for CPT
- How to apply order on custom taxonomy and custom meta key on custom post type
- How to change post cpt with submit button?
- I am having a problem with fetching product data in the Gutenberg block editor
- Post URL redirecting to homepage
- Customize permalink when creating a post
- How to make post sticky in the admin page?
- get_template_part for custom post type content not working
- Custom Taxonomy Not Showing in Menu
- Remove All, Published and Trashed Post Views in Custom Post Type
- Custom Post Type | Fatal Error on register_post_type()
- Loop on front-page.php
- Set object terms not working on plugin activation
- Custom Taxonomy – Tags Metabox is showing instead of Categories
- Custom query shows custom post types in trash
- Why my custom post posts aren’t showing (404 error / page not found)
- Multiple post type queries (with specific arguments for each)
- Using custom page templates with plugin [duplicate]
- Keyword search limited to specific post type filtered by multiple custom taxonomies?
- Automaticly add slug to posts
- need advice on how to do a lists using custom post types – taxonomy vs postmeta
- How to post twitter like updates on wordpress
- Show Two custom Post type and their posts on category page
- How to filter search results by post type?
- How to create an advanced filter search?
- WP_Query is printing out only one post when posts_per_page is set to multiple
- Trying to edit the single page from a Custom Post
- How to Submit Post Via cURL & PHP
- Custom search for custom post meta with pre_get_posts interferes with WP search
- How to get all posts except for one post from a certain custom post type?
- count_many_users_posts except current logged in user?
- Get all of user’s custom post types in WP Admin for plugin
- How to search map address in custom post type?
- How can I show a custom post type for users in the authors.php file?
- Issue with pre_get_posts on custom post type archive pages
- Blog page showing same content as homepage
- Custom Post Type Navigation on Custom Field
- apply filters only to specific post listing without check the url parameters
- Combine query_posts() and get_posts() into single query
- how can i get posts from custom post type particular taxonomy category
- How Can I Set the Post Author of a Post I Just Created With PHP?
- Custom post type and body_class: Remove “blog” class
- WordPress Orderby Numeric Value Not Working
- Setting posts per page in query_posts
- How to insert content from another Custom Post type into Post?
- Custom search results page query, link permalink to post title while separating by post types
- Plugin translation not working apart from name and description
- Get rewrite slug of custom post type in template
- Exclude child pages from archive.php
- is there a way to retrieve posts that do not have a featured image assigned?
- Auto-generated posts not showing in backend (but being counted!)
- Custom Post Type rewrite
- Shortcode for display posts on home page show only one post
- Custom Search for Drafts in Custom Post Type
- Deleting Custom Post type data using mySQL command
- Admin notice not displaying
- Pagination is not working on single-{slug}.php but works fine on page-{slug}.php
- How to Get Current Custom Post Type Selected Taxonomy Term (Not All Terms)
- Find the user who initially created the post