英文:
Updating the page creation code in WordPress (get_page_by_title() deprecated)
问题
我使用以下代码在安装我的主题后创建页面。
$check_page_exist = get_page_by_title('test', 'OBJECT', 'page');
if(empty($check_page_exist)) {
$page_id = wp_insert_post(
array(
'comment_status' => 'close',
'ping_status' => 'close',
'post_author' => 1,
'post_title' => ucwords('test'),
'post_name' => strtolower(str_replace(' ', '-', trim('test'))),
'post_status' => 'publish',
'post_content' => ' ',
'post_type' => 'page',
'post_parent' => ' '
)
);
}
这段代码能够正常工作并生成我需要的页面。
这段代码唯一的问题与`get_page_by_title()`部分有关。在安装并运行Query Monitor插件后,它会显示一个消息,说明这个项目已经过时。
在以前的问题中,我收到了一个与“get_page_by_title()已弃用”错误相关的页面地址:
[make.wordpress.org/core/2023/03/06/get_page_by_title-deprecated][1]
如果可能的话,请编辑我的代码,以便不再向我显示此消息,并像以前一样创建我想要的页面。
非常感谢。
**编辑后的代码:**
$check_page_exist = new WP_Query( array(
'pagename' => 'test',
'posts_per_page' => 1,
'no_found_rows' => true,
'fields' => 'ids',
) );
if ( empty( $check_page_exist->posts ) ) {
$page_id = wp_insert_post(
array(
'comment_status' => 'close',
'ping_status' => 'close',
'post_author' => 1,
'post_title' => ucwords('test'),
'post_name' => strtolower(str_replace(' ', '-', trim('link'))), // 我已更改此页面的所需链接
'post_status' => 'publish',
'post_content' => '',
'post_type' => 'page',
'post_parent' => ' '
)
);
}
[1]: https://make.wordpress.org/core/2023/03/06/get_page_by_title-deprecated
英文:
I use the following code to create the page after installing my theme.
$check_page_exist = get_page_by_title ('test', 'OBJECT', 'page');
if(empty($check_page_exist)) {
$page_id = wp_insert_post(
array(
'comment_status' => 'close',
'ping_status' => 'close',
'post_author' => 1,
'post_title' => ucwords('test'),
'post_name' => strtolower(str_replace(' ', '-', trim('test'))),
'post_status' => 'publish',
'post_content' => ' ',
'post_type' => 'page',
'post_parent' => ' '
)
);
}
This code works correctly and generates the pages I need.
The only problem this code has is related to the get_page_by_title()
part. After installing and running the Query Monitor plugin, it gives me a message that this item is obsolete.
In the previous question, I was sent a page address that still did not solve my problem.
The address of the page related to the get_page_by_title() deprecated
error:
make.wordpress.org/core/2023/03/06/get_page_by_title-deprecated
Please, if possible, edit my code so that this message is no longer displayed to me and it creates the page I want as before.
Many thanks.
Edit code:
$check_page_exist = new WP_Query( array(
'pagename' => 'test',
'posts_per_page' => 1,
'no_found_rows' => true,
'fields' => 'ids',
) );
if ( empty( $check_page_exist->posts ) ) {
$page_id = wp_insert_post(
array(
'comment_status' => 'close',
'ping_status' => 'close',
'post_author' => 1,
'post_title' => ucwords('test'),
'post_name' => strtolower(str_replace(' ', '-', trim('link'))), // I have changed my desired link for this page
'post_status' => 'publish',
'post_content' => '',
'post_type' => 'page',
'post_parent' => ' '
)
);
}
答案1
得分: 1
对于 get_page_by_title()
的文档 建议使用 WP_Query
作为替代。
未经测试的代码:
$check_page_exist = new WP_Query( array(
'pagename' => 'test',
'posts_per_page' => 1,
'no_found_rows' => true,
'fields' => 'ids',
) );
if ( empty( $check_page_exist->posts ) ) {
// 创建您的页面。
}
英文:
Documentation for get_page_by_title()
suggests using WP_Query
as replacement.
Untested code:
$check_page_exist = new WP_Query( array(
'pagename' => 'test',
'posts_per_page' => 1,
'no_found_rows' => true,
'fields' => 'ids',
) );
if ( empty( $check_page_exist->posts ) ) {
// Create your page.
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论