admin管理员组文章数量:1424606
I get error when i use get_page_by_path() with WP_Query in a php class. I use after_switch_theme and switch_theme in that class. And while the theme is activated then everything is fine as expected. But the problem is, while i switching theme then browser show me an error.
Catchable fatal error: Object of class WP_Query could not be converted to string in C:\wamp\www\wordpress-theme-test\wp-includes\post.php on line 4470
I try to figure out the error `
public function theme_deactivation() {
$pages = array( 'member-login', 'member-account', 'member-register', 'member-password-lost', 'member-password-reset' );
foreach ( $pages as $slug ) {
$query = new WP_Query( 'pagename=' . $slug );
$page = get_page_by_path( $query );
wp_delete_post( $page->ID, true );
}
}
And I think, the main problem is get_page_by_path(). It's first and required parameter is $page_path (string) and WP_Query is an object. And error say WP_Query could not be converted to string (for get_page_by_path())
Is there any way to solve the problem??? Thanks.
I get error when i use get_page_by_path() with WP_Query in a php class. I use after_switch_theme and switch_theme in that class. And while the theme is activated then everything is fine as expected. But the problem is, while i switching theme then browser show me an error.
Catchable fatal error: Object of class WP_Query could not be converted to string in C:\wamp\www\wordpress-theme-test\wp-includes\post.php on line 4470
I try to figure out the error `
public function theme_deactivation() {
$pages = array( 'member-login', 'member-account', 'member-register', 'member-password-lost', 'member-password-reset' );
foreach ( $pages as $slug ) {
$query = new WP_Query( 'pagename=' . $slug );
$page = get_page_by_path( $query );
wp_delete_post( $page->ID, true );
}
}
And I think, the main problem is get_page_by_path(). It's first and required parameter is $page_path (string) and WP_Query is an object. And error say WP_Query could not be converted to string (for get_page_by_path())
Is there any way to solve the problem??? Thanks.
Share Improve this question edited Jun 8, 2019 at 12:35 Zahid Hossain asked Jun 8, 2019 at 12:24 Zahid HossainZahid Hossain 71 silver badge4 bronze badges1 Answer
Reset to default 1It doesn't make any sense to try and pass a query result to get_page_by_path()
. The point of WP_Query
is to query for a selection of posts based on given criteria, while get_page_by_path()
is for retrieving a single specific post based on its path. There's no reason you'd need to use them together.
If you want to delete posts based on their slug, then you don't need a query at all:
$pages = array( 'member-login', 'member-account', 'member-register', 'member-password-lost', 'member-password-reset' );
foreach ( $pages as $slug ) {
$page = get_page_by_path( $slug );
if ( $page ) {
wp_delete_post( $page->ID, true );
}
}
本文标签: wp queryProblem with getpagebypath() using with WPQuery
版权声明:本文标题:wp query - Problem with get_page_by_path() using with WP_Query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745424754a2658058.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论