WordPress.org

Ready to get started?Download WordPress

Forums

Trigger ADD_ACTION only on Publish of a Custom Page Type (1 post)

  1. Cyanide Sabre
    Member
    Posted 1 year ago #

    I've bought a commercial theme to use as part of a photographic website. The theme comes with a couple of CPT's - one of which is a Portfolio.

    The CPT has custom fields - one of which is "lightbox-url" that ensures my featured image is displayed in a light box (actually prettymedia plug in).

    I've added some code to functions.php to ensure that the custom field for lightbox-url is completed when the Post is first published and all seems well.

    My issue is that I would also like to be able to remove the entry for the custom field "lightbox-url". When I select DELETE the custom field and UPDATE the post, the rul value is re-populated.

    I've tried checking WP-IS-POST-REVISION and POST_DATE<>MODFIED_DATE but no joy.

    Anyone any idea?

    Current version of my code is like this?

    add_action('publish_portfolio', 'add_custom_field_automatically',10,2);
    function add_custom_field_automatically($post_ID) {
      global $wpdb;
    // Not for Revisions
      if ( !wp_is_post_revision($post_ID) ) {
        $post_thumbnail_id = get_post_thumbnail_id();
        $post_thumbnail_url = wp_get_attachment_url($post_thumbnail_id);
        add_post_meta($post_ID, 'lightbox-url', $post_thumbnail_url, false);
      }
    }

    [Moderator Note: Please post code or markup snippets between backticks or use the code button. As it stands, your code may now have been permanently damaged/corrupted by the forum's parser.]

Topic Closed

This topic has been closed to new replies.

About this Topic