When the new post which has no image published, save the specific image as the featured image ( by category )

As the title, I would like to save the post with the specific image as the featured image, when it is new published. Also I would like to save different images which is filtered by categories.

I have wrote the code below, it does not work as I wish to.

add_action('save_post', 'wp_force_featured_image', 20, 2);

function wp_force_featured_image($post_id, $post) {

    if( $post->post_type == 'post' && $post->post_status == 'publish' ) {
    
        if(!isset($_POST['_thumbnail_id'])) {       
            $categories = get_the_category( $post->slug );  
             if ( $categories = 'news' ) {      
              add_post_meta( $post_id, '_thumbnail_id', '3135' );
             }
             elseif  ($categories = 'bi' ) {
              add_post_meta( $post_id, '_thumbnail_id', '3138' );
             }
        }
        }
    }

I tried to get the category slug for comparing.

Any advises will help me a lot.

Thank you for your support in advance.

Solutions

If you're building a custom theme and you just want to have some fallback images, an easier approach might be to do something like this in your template:

<?php
if (has_post_thumbnail()) {
  the_post_thumbnail();
} else if (in_category('news') {
  echo wp_get_attachment_image('3135');
} else if (in_category('bi') {
  echo wp_get_attachment_image('3138');
}

?>

Depending on what you're trying to do, this may or may not be a solution.

Your conditional is in trouble.

get_the_category() returns an object , use foreach to find the specific category.

In addition, you assigned a value to the $categories variable, to compare you must use the comparison operators ( Ex: == or === )

I refactored the code and adapted it to your case, I hope it helps.

add_action( 'save_post', 'wp_force_featured_image', 10, 3 );

function wp_force_featured_image( $post_id, $post, $update ) {
    if ( $update ) {
        return;
    }

    if ( $post->post_type !== 'post' ) {
        return;
    }

    if ( $post->post_status !== 'publish' ) {
        return;
    }

    $has_post_thumbnail = get_post_thumbnail_id( $post_id );

    if ( ! empty( $has_post_thumbnail ) ) {
        return;
    }

    $categories = get_the_category( $post_id );

    $thumbnail_id = false;
    
    foreach ( $categories as $category ) {
        if ( $category->slug === 'news' ) {
            $thumbnail_id = 3135;
            break;
        }

        if ( $category->slug === 'bi' ) {
            $thumbnail_id = 3138;
            break;
        }
    }

    if( $thumbnail_id ) {
      add_post_meta( $post_id, '_thumbnail_id', $thumbnail_id, true );
    }
}

Similar questions

How to know which product variation has which Variation ID in WooCommerce REST API
I'm using WooCommerce Rest APIs in wordpress to make an android application and i'm trying to get product variation from the product detail's response.I am getting product variations with its attributes and names in a list but i don't know how to check which variation has which Variation ID. here i have names and ID of attributes with everything an...
Send email with custom fields after new draft is saved or new post published
I have a custom post type immobile with some custom fields created with Toolset Types. I need to send and email with the title and a custom field when a new immobile is saved as a draft or it's published. I managed to do almost everthing. My code is like this: Unfortunately my custom field is not showing in the email, probably because It get saved ...
Immediately published posts are scheduled and published 2 hours later
If I try to publish posts immediately, they get scheduled for the current time but are published 2 hours later. Also scheduled posts are published 2 hours after the planned time. In think its a timezone issue but I've no clue where to look else: Wordpress Setting: PHP Setting: I'm on Ubuntu 16.04 and the timezone seems to set there correct, too. my...
Check if current user has a post and that post has any term/s from a specific custom taxonomy outside the loop
I'm looking for a way to check if (whilst outside the loop) the current user.. 1. is logged in 2. has a post published 3. their post has any term/terms from a custom taxonomy I have this so far... I'm just wildly guessing here but could I use if( has_term( '', 'custom-taxonomy' ) )? but that's for use inside the loop.
How to display an icon when a new post is published and then remove it when a specific time past?
Every time i publish a new post i want to display an icon like a badge (NEW) and after a specific time it will disappear. Lets say i publish a new post and i want to display that it is a new post. So i want to insert an icon that for example a star, in each new post in the blog list view into the metadata of the posts. Also i want to be removed aft...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .