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

permalinks - Trying to get property of non-object when using get_post_permalink()

programmeradmin4浏览0评论

I'm trying to get the permalink of a custom post type using get_post_permalink(). I need to use get_post_permalink() because I use the post_type_link filter to change the permalink for specific post types to something other than the usual. Here is the code that I'm trying to run:

class Get_Department_Class {

/**
 * The department's post ID.
 *
 * @since    1.0
 * @access   public
 * @var      int    $ID    Holds the department's post ID
 */
public $ID;

/**
 * Will hold the department's post data.
 *
 * @since    1.0
 * @access   public
 * @var      array    $post    Holds the department's post data
 */
public $post;

/**
 * Will hold the department's permalink.
 *
 * @since    1.0
 * @access   protected
 * @var      string    $permalink    Holds the department's permalink
 */
protected $permalink;

/**
 * Build/construct the department.
 *
 * @since   1.0
 * @param   $post_id - allows you to pass the department's post ID or post title
 */
public function __construct( $post_id ) {
    global $wpdb, $blog_id;

    // If not main site, change $wpdb blog ID for queries
    // Will change back when we're done
    $old_blog_id = ( 1 != $blog_id ) ? $wpdb->set_blog_id( 1 ) : NULL;


    // Check against an ID
    if ( is_numeric( $post_id ) ) {

        $this->post = get_post( $post_id );

    }

    // Check against a post title
    if ( ! $this->post ) {

        $this->post = get_page_by_title( $post_id, OBJECT, 'departments' );

    }

    // Reset the blog ID
    if ( isset( $old_blog_id ) )
        $wpdb->set_blog_id( $old_blog_id );


    // No point if there's no post ID
    if ( ! ( $this->ID = isset( $this->post ) && isset( $this->post->ID ) ? $this->post->ID : NULL ) ) {
        $this->post = NULL;
        return;
    }

}

/**
 * Get the department's permalink.
 *
 * @since   1.0
 */
public function get_permalink() {

    // Make sure we have the post ID
    if ( ! $this->ID )
        return false;

    // See if the permalink is already set
    if ( isset( $this->permalink ) && ! empty( $this->permalink ) )
        return $this->permalink;

    // Get the permalink
    if ( $permalink = get_post_permalink( $this->ID ) ) {

        // Store the permalink
        $this->permalink = $permalink;
        return $this->permalink;

    }

    return false;

}
}

The error that is returned is "Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/wp-includes/link-template.php on line 266." After doing some testing in the wordpress core, line 264 in the link-template.php file is returning NULL.

line 264:

        $post_type = get_post_type_object($post->post_type);

Then on line 266 the core code attempts to access a property of the $post_type object which is NULL and therefore throws the error.

I don't want to modify the core code. Any ideas on how I can fix this?

FYI: I'm running WP 4.5.9...

I'm trying to get the permalink of a custom post type using get_post_permalink(). I need to use get_post_permalink() because I use the post_type_link filter to change the permalink for specific post types to something other than the usual. Here is the code that I'm trying to run:

class Get_Department_Class {

/**
 * The department's post ID.
 *
 * @since    1.0
 * @access   public
 * @var      int    $ID    Holds the department's post ID
 */
public $ID;

/**
 * Will hold the department's post data.
 *
 * @since    1.0
 * @access   public
 * @var      array    $post    Holds the department's post data
 */
public $post;

/**
 * Will hold the department's permalink.
 *
 * @since    1.0
 * @access   protected
 * @var      string    $permalink    Holds the department's permalink
 */
protected $permalink;

/**
 * Build/construct the department.
 *
 * @since   1.0
 * @param   $post_id - allows you to pass the department's post ID or post title
 */
public function __construct( $post_id ) {
    global $wpdb, $blog_id;

    // If not main site, change $wpdb blog ID for queries
    // Will change back when we're done
    $old_blog_id = ( 1 != $blog_id ) ? $wpdb->set_blog_id( 1 ) : NULL;


    // Check against an ID
    if ( is_numeric( $post_id ) ) {

        $this->post = get_post( $post_id );

    }

    // Check against a post title
    if ( ! $this->post ) {

        $this->post = get_page_by_title( $post_id, OBJECT, 'departments' );

    }

    // Reset the blog ID
    if ( isset( $old_blog_id ) )
        $wpdb->set_blog_id( $old_blog_id );


    // No point if there's no post ID
    if ( ! ( $this->ID = isset( $this->post ) && isset( $this->post->ID ) ? $this->post->ID : NULL ) ) {
        $this->post = NULL;
        return;
    }

}

/**
 * Get the department's permalink.
 *
 * @since   1.0
 */
public function get_permalink() {

    // Make sure we have the post ID
    if ( ! $this->ID )
        return false;

    // See if the permalink is already set
    if ( isset( $this->permalink ) && ! empty( $this->permalink ) )
        return $this->permalink;

    // Get the permalink
    if ( $permalink = get_post_permalink( $this->ID ) ) {

        // Store the permalink
        $this->permalink = $permalink;
        return $this->permalink;

    }

    return false;

}
}

The error that is returned is "Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/wp-includes/link-template.php on line 266." After doing some testing in the wordpress core, line 264 in the link-template.php file is returning NULL.

line 264:

        $post_type = get_post_type_object($post->post_type);

Then on line 266 the core code attempts to access a property of the $post_type object which is NULL and therefore throws the error.

I don't want to modify the core code. Any ideas on how I can fix this?

FYI: I'm running WP 4.5.9...

Share Improve this question edited Sep 12, 2017 at 13:09 TKEz asked Sep 11, 2017 at 20:32 TKEzTKEz 1111 silver badge10 bronze badges 10
  • Looks like $this is not an object, maybe you want to use $post? – Mihai Papuc Commented Sep 11, 2017 at 20:34
  • Sorry, I only posted a small portion of the code. I should have put more. So the block of code with $this->permalink is actually within a class. The file that's throwing the error is link-template.php which is erroring because $post_type (on line 264) is not an object. – TKEz Commented Sep 11, 2017 at 20:37
  • If you simply return $permalink instead of return $this->permalink; do you still get the error? Comment out the $this->permalink = $permalink; just incase. – Jeff Mattson Commented Sep 11, 2017 at 20:40
  • Can you post more of the code? – Jeff Mattson Commented Sep 11, 2017 at 20:47
  • Perhaps you haven't called global $post to be able to access it? – WebElaine Commented Sep 11, 2017 at 21:48
 |  Show 5 more comments

1 Answer 1

Reset to default 0

The reason that it's an issue is because I'm using switch_to_blog() on a multisite. The post_type doesn't exist on the current blog. It exists only on the main blog. Since the post_type doesn't exist, the get_post_type_object() function is returning null.

What I did to fix this is create a custom_get_post_permalink function that also takes a blog_id argument and therefore changes to the appropriate blog_id on the multisite to create the post_type_object correctly.

发布评论

评论列表(0)

  1. 暂无评论