2

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...

10
  • Looks like $this is not an object, maybe you want to use $post? 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. Commented Sep 11, 2017 at 20:40
  • Can you post more of the code? 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

1 Answer 1

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.

1
  • You can mark your answer as the correct answer.
    – Hector
    Commented Nov 24, 2019 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.