最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Problem with get_page_by_path() using with WP_Query

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

It 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 );
    }
}
发布评论

评论列表(0)

  1. 暂无评论