PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 5, 2022

[FIXED] How to make Wordpress save_post_($post-type) hook works with multiple Custom Post Types

 February 05, 2022     advanced-custom-fields, php, wordpress, wordpress-hook     No comments   

Issue

I have an issue related save_post_($post-type) hook and ACF gallery field.

I got this code snippet from somewhere. Let me explain: I created an ACF gallery field (acf-gallery is the field name) shown in Custom Post Type (cpt1 is the slug) then use this snippet to set the first image of this gallery as the featured image when saving just like what Woocommerce do.

But what if I want it to work with another Custom Post Type (let's say the slug is cpt2)? Can I use array( 'cpt1', 'cpt2' ) to replace cpt1? Is there a way to include multiple custom post types?


/* Set the first image generated by ACF gallery field as featured image */

add_action( 'save_post_cpt1', 'set_featured_image_from_gallery' );

function set_featured_image_from_gallery() {

    global $post;
    $post_id = $post->ID;

    $images = get_field('acf_gallery', $post_id, false);
    $image_id = $images[0];

    if ( $image_id ) {
        set_post_thumbnail( $post_id, $image_id );
    }
}

I edited this snippet using save-post hook according to comments below. But I don't know if it's valid. Can someone help?

/* Set the first image generated by ACF gallery field as featured image */

add_action( 'save_post', 'set_featured_image_from_gallery' );

function set_featured_image_from_gallery($post_id) {

    if (get_post_type($post_id) != array( 'cpt1', 'cpt2')) {
        return;
    }

    $has_thumbnail = get_the_post_thumbnail($post_id);

      if ( !$has_thumbnail ) {

        $images = get_field('acf_gallery', $post_id, false);
        $image_id = $images[0];

        if ( $image_id ) {
          set_post_thumbnail( $post_id, $image_id );
        }
      }
}

Solution

I usually use save_post hook, $post_id variable, get_post_type and in_array functions.

function set_featured_image_from_gallery($post_id)
{
    $included_cpts = array('cpt1', 'cpt2', 'cpt3');

    if (in_array(get_post_type($post_id), $included_cpts)) {

        $has_thumbnail = get_the_post_thumbnail($post_id);

        if (!$has_thumbnail) {

            $images = get_field('acf_gallery', $post_id, false);

            $image_id = $images[0];

            if ($image_id) {

                set_post_thumbnail($post_id, $image_id);
                
            }
        }
    }
}

add_action('save_post', 'set_featured_image_from_gallery');


Answered By - Mokhless
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing