Default featured image for 'POST' type only (exclude 'page')

275 views Asked by At

Hello I am trying to have a default featured image set for post_type = 'post' only excluding post_type = 'page'.

I wrote the following code in the functions file of the child theme but i keep getting this error:

Notice: Trying to get property of non-object in /home/ossettto/public_html/wp-content/themes/sport-child/functions.php on line 18

function wpforce_featured()
{
    global $post;
    $post_type = get_post_type($post->ID);
    if ($post_type === 'post')
    {
        $already_has_thumb = has_post_thumbnail($post->ID); // If post have a featured image use that.
        if (!$already_has_thumb)
        {
            //If post does not have a featured image then get the first post image and set as featured image.
            $attached_image = get_children("post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1"); // Number 1 relates to taking post image number 1 and adding it as a featured image.
            if ($attached_image)
            {
                foreach ($attached_image as $attachment_id => $attachment)
                {
                    set_post_thumbnail($post->ID, $attachment_id);
                    //$attachment_id = attachment_url_to_postid( $image_url );
                    //echo $attachment_id;
                }
            }
            else
            {
                set_post_thumbnail($post->ID, '27264'); // Find attachment media id by right clicking the image in the Media library and selecting inspect element. Look for the data-id number. This number is then added to the post id.
            }
        }
    }
}

//end function
add_action('the_post', 'wpforce_featured');
add_action('save_post', 'wpforce_featured');
add_action('draft_to_publish', 'wpforce_featured');
add_action('new_to_publish', 'wpforce_featured');
add_action('pending_to_publish', 'wpforce_featured');
add_action('future_to_publish', 'wpforce_featured');

Any help will be appreciated.
Thanks.

1

There are 1 answers

0
random_user_name On

Two things are unclear:

  1. Why you are to do this in the_post, save_post, and the other hooks all together.
  2. It would be helpful to know which line is line 18, but I'm guessing it's this line: $post_type = get_post_type( $post->ID );.

However, the reason you are getting the notice is that these actions don't necessarily all have the $post object ready for you to global $post. Further, these actions all have different function signatures, passing $post as a parameter in different places.

Given all the filters you are hooking into, you need to create an "abstraction" or "wrapper" around your function so that you can call it properly with $post in the proper position of the arguments.

Look at the docs to see examples of where $post is being passed:

the_post action - passes as ONLY parameter
save_post action - passes as SECOND parameter
draft_to_published (and other hooks) - passes as THIRD parameter

// New function that accepts proper parameters for save action
function wpforce_featured_on_save( $post_id, $post, $update ) {
    // No need to duplicate code.  Instead, call your original function
    // passing it the $post parameter
    wpforce_featured_status( $post );
}

// New function that accepts proper parameters for these actions
function wpforce_featured_on_status_change( $new, $old, $post ) {
    // No need to duplicate code.  Instead, call your original function
    // passing it the $post parameter
    wpforce_featured( $post );
}

// Your original function with slight modifications
// NOTE: ONLY accepts $post object - no global post
function wpforce_featured( $post ) {
      // REMOVED global $post - not helpful here
      $post_type = get_post_type( $post->ID );
      // ... the rest of your code here
}

// Your original hook, it's passing the $post_object parameter
add_action('the_post', 'wpforce_featured');
// The save hook, which passes parameters - modified to call a different function
add_action('save_post', 'wpforce_featured_save', 10, 3);
// The status change hooks, which pass parameters - modified to call a different function
add_action('draft_to_publish', 'wpforce_featured_on_status_change', 10, 3);
add_action('new_to_publish', 'wpforce_featured_on_status_change', 10, 3);
add_action('pending_to_publish', 'wpforce_featured_on_status_change', 10, 3);
add_action('future_to_publish', 'wpforce_featured_on_status_change', 10, 3);