admin管理员组文章数量:1321074
I'm developing a site where people can search phone number(s), the most complex part is that i want to create a post automatically when someone search for a specific number if that phone number yield zero results want to create a post with title as the searched number. this should be done without any user registration process. is there a way to achieve this?
I'm developing a site where people can search phone number(s), the most complex part is that i want to create a post automatically when someone search for a specific number if that phone number yield zero results want to create a post with title as the searched number. this should be done without any user registration process. is there a way to achieve this?
Share Improve this question asked Sep 30, 2020 at 3:59 greenarrowgreenarrow 133 bronze badges 1- Just a word of caution here, this has the potential of taking your site down. If your site is flooded with searches, you'll be creating a lot of posts. What is the intent behind this approach? – Welcher Commented Sep 30, 2020 at 13:29
1 Answer
Reset to default 0Assuming 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.
本文标签: plugin developmentCreate a post automatically if search result has zero results
版权声明:本文标题:plugin development - Create a post automatically if search result has zero results 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742091757a2420309.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论