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

Question

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
 * Yavar Mammadov      int    $ID    Holds the department's post ID
 */
public $ID;

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

/**
 * Will hold the department's permalink.
 *
 * @since    1.0
 * @access   protected
 * Yavar Mammadov      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…

0
TKEz 4 years 2017-09-11T15:32:03-05:00 0 Answers 106 views 0

Leave an answer

Browse
Browse