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?
- Display a query with multiple post types and same relationship on a single page
- Why get_next_post_link() or get_previous_post_link() doesn’t return the required links?
- Add metabox with media uploader in a custom post type [duplicate]
- Retrieving 3 latest post from each of 5 different custom post types
- automatically create taxonomy with same name as post title
- Wp-query causing problems with the_content();
- saving custom post type data to different table in wordpress
- Display “Post 2 of 4” on single post page?
- Limit Authors to their Own Posts on front-end excluding admins
- Remove Post Page “View Post” Link
- Make custom post type display as a page
- Get permalink and title by post name?
- reference the current category being used in the category.php page
- Add custom post type archives to search results?
- CPT Meta Searching
- How to make post sticky in the admin page?
- How to filter search results by post type?
- How to customize a permalink (URL) structure?
- Archive for custom taxonomy lists all posts instead of current taxonomy
- search also in taxonomy, tags and custom fields
- Which post does a taxonomy term belongs to?
- Add custom field to Posts and sort by it
- Addition of custom option panel crashes Media Library & Admin Area
- Including metaboxes from custom post types in global search — continued
- help
- How to Display Posts From Category Within a Custom Taxonomy?
- post-per-page and offset not working
- Published post auto change status to pending after 90 days
- How to retrieve all wordpress posts (specific type) inside a function in php?
- Custom Meta Box returns no HTML
- Automatically update slug with latest title within custom post type [duplicate]
- How to Query in WordPress which shows Alphabetic Posts?
- Display custom post counts for author , not default posts
- Display all Categories except ones with a specific parent
- allowing custom user role to access custom post type in wordpress admin
- Trying to list years (for a filter) but not all years display
- Register custom_post_type from static function
- how to display posts content on the custom css popup by clicking on each title on the sidebar?
- How to select meta key in custom database query
- How do I insert a custom post type query after a certain number of recent posts and then resume recent posts?
- subtracting the current post form then whole loop, which is generating all CPT titles
- How to seperate posts by categories?
- Assign for all post of a post type a specific single-post template
- Search in multiple specific post types
- How can I load Template file from wordpress plugin
- custom search form for custom post
- Custom post url in search.php isn’t the correct rewrited url
- Show custom post type under last posts configuration
- Manually build WP_Query
- Optimise custom post type queries
- delete custom post type using a submit form
- Problem with custom post type search
- Styling first post using Advanced Custom Fields
- Query custom type posts by array of IDs
- How to make a non-public post on wordpress approval?
- Quering array of post types & pagination. Articles are repeating sometimes on different pages