• Notice: Trying to get property of non-object in D:\Server2Go\htdocs\wordpress-nl\wordpress\wp-content\plugins\autoset-featured-image\autoset-featured-image.php on line 38

    Notice: Trying to get property of non-object in D:\Server2Go\htdocs\wordpress-nl\wordpress\wp-content\plugins\autoset-featured-image\autoset-featured-image.php on line 40

    Notice: Trying to get property of non-object in D:\Server2Go\htdocs\wordpress-nl\wordpress\wp-content\plugins\autoset-featured-image\autoset-featured-image.php on line 42

    Notice: Trying to get property of non-object in D:\Server2Go\htdocs\wordpress-nl\wordpress\wp-content\plugins\autoset-featured-image\autoset-featured-image.php on line 45

Viewing 2 replies - 1 through 2 (of 2 total)
  • Jayne

    (@ict-made-easy)

    I am not using this plugin but came across a similar error on my own custom autoset featured image, which I have modified from here.

    I have included a few extra lines which means you don’t get the debug errors on posts that haven’t been created yet e.g. on new-post.php and new-page.php admin screens

    The following may help you:

    Create a new file called and name it something like featured-image.php and save somewhere in your themes folder

    Then include a reference to if from your functions.php file e.g. (replace /inc/ with your path to the file)

    require get_template_directory() . '/inc/featured-image.php';

    Then in the featured-image.php file drop the following code

    /**
     * Autoset featured image to first image in post if not specifically defined
     */
    add_action('the_post', 'your_theme_autoset_featured_image');
    add_action('save_post', 'your_theme_autoset_featured_image');
    add_action('draft_to_publish', 'your_theme_autoset_featured_image');
    add_action('new_to_publish', 'your_theme_autoset_featured_image');
    add_action('pending_to_publish', 'your_theme_autoset_featured_image');
    add_action('future_to_publish', 'your_theme_autoset_featured_image');
    function your_theme_autoset_featured_image() {
    
    		//checks for thumbnail - it does not use the global post ID because we haven't created a post yet!
    		if ( !has_post_thumbnail() ){
    
    			// if post does not have a thumbnail then do nothing!!
    			}
    
    		else {
    
    			// set globals for the function
    			global $post;
    			$already_has_thumb = has_post_thumbnail($post->ID);
    			$screen = get_current_screen();
    
    	/*
    	* checks that you are not on add a new post or add new page screen, so it is not looking for empty objects and thus returning no debug errors! Yay!
    	*/
    		// check for thumb and what admin page you are on
    		if (!$already_has_thumb && $screen->base !== '-new.php')  {
    
    			$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
    
    			if ($attached_image) {
    				foreach ($attached_image as $attachment_id => $attachment) {
    				set_post_thumbnail($post->ID, $attachment_id);
    				}
    			}
    
    		}
    	}
    }

    The crucial bits in the code above are

    if ( !has_post_thumbnail() ){
    // if post does not have a thumbnail then do nothing!!
    }

    and

    if (!$already_has_thumb && $screen->base !== '-new.php')  {
    //original code
    }

    You might be able to modify the plugin php files with the extra bits Hope this helps

    Jayne

    (@ict-made-easy)

    Please ignore the above!
    Sorry – it’s got some errors in it – will work on fixing it and post back

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Error Messages in WP4; please update!’ is closed to new replies.