0

I'm using a post crawler on my website that pulls articles from other websites and automatically publishes them on my website. During that process, it checks for any video embed codes; if they exist, the crawler created a custom field "video_code" to the post and adds the video embed code in the value.

I wrote a function that checks the crawled posts for a specific custom field called "video_code". If that custom field exists, it changes the post format from Standard to Video.

    function check_meta( $post_id, $post, $update ) {
        $post_type = get_post_type($post_id);
        if ( "post" != $post_type ) return;

        if ( isset( $_POST['video_code'] ) ) {
            set_post_format( $post_id, 'video' );
        }
    }
    add_action( 'save_post', 'check_meta', 999, 3 );

The function above works only when i edit the posts and manually click the Update button and the reason is that at the time the function checks for the meta data of the post, the meta data does not exist.

How can i run the above the function, AFTER a post has been saved, to make sure that the meta exists and my function can check the meta and change the post format.

UPDATE: I contacted the plugin author, although he couldn't help me write the code, he did give me some points on how to make this work.

His reply:

At the time you check the meta data of the post, the meta data does not exist. The plugin has filters and actions you can use. You can see https://pastebin.com/jLmnp2F8 file's insertPostData() method. There are 1 filter and 2 actions you can use. For example, you can use wpcc/post/after_save action. The first parameter of the action provides a PostData instance, from which you can retrieve the custom meta values. See https://pastebin.com/u8t2cf4P file's getCustomMeta() method. It provides the custom meta as key-value pairs. The action also provides post ID as a parameter. For the parameters and their descriptions, you can see the insertPostData() method.

  • And how do you publish these posts if not manually? When do you set that custom field? – Krzysiek Dróżdż Feb 5 at 8:51
  • I think what Krzysiek Dróżdż may be getting at is that you could, presumably, set the desired post_format (or anything else) when the post was originally inserted or published, however you're accomplishing that. Why isn't that an option here? – CK MacLeod Feb 5 at 8:55
  • I edited my question. The post is automatically published by the crawler and the meta_key is set during the crawling function. – user2093301 Feb 5 at 8:59
  • Have you tried to hook on other event, try add that filter on wp_insert_post – hamdirizal Feb 5 at 9:09
  • I've changed the add action to add_action( 'wp_insert_post', 'check_meta', 999, 3 ); but that didn't work either; or do you mean to wrap my function in another wp_insert_post function? – user2093301 Feb 5 at 9:14
0

function check_meta( $post_id, $post, $update ) {

        $post_type = get_post_type($post_id);
        if ( "post" != $post_type ) return;


        $video_code_val = get_post_meta( $post_id, 'video_code', true );

        // Check if the video_code field has a value.
        if ( ! empty( $video_code_val ) ){

         //if it does, set post format
         set_post_format( $post_id, 'video' );

        }

}

add_action( 'save_post', 'check_meta', 999, 3 )

Maybe also try other actions...

If the post is saved via published:

add_action( 'publish_post', 'check_meta', 999, 3 );

or if on if a post is saved as draft:

add_action( 'draft_to_publish', 'check_meta', 999, 3 );

or this updated_post_meta action might work:

add_action( 'updated_post_meta', 'check_meta', 999, 3 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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