• Hi,
    Since I am using a theme that requires a Custom Image Field in each and every post, I need to make sure my authors put one in. The thing is, I want to have a default image in case they don’t put it in (a custom field automatically added ONLY IF one with the same name doesn’t already exist).

    I saw the code online to auto-add, but does that code add the custom field even if one already exists there? That’s what I don’t want, I want to add a custom field IF AND ONLY IF a custom field with the same name doesn’t already exist. Any ideas?

Viewing 1 replies (of 1 total)
  • Thread Starter futurepocket

    (@futurepocket)

    add_action(‘publish_page’, ‘add_custom_field_automatically’);
    add_action(‘publish_post’, ‘add_custom_field_automatically’);
    function add_custom_field_automatically($post_ID) {
    global $wpdb;
    if(!wp_is_post_revision($post_ID)) {
    add_post_meta($post_ID, ‘field-name’, ‘custom value’, true);
    }
    }

    That is the code I basically need, but I am a complete beginner at PHP, so where and how would I add an “if” command in there to only add the custom field if one doesn’t exist already? Thanks!

Viewing 1 replies (of 1 total)
  • The topic ‘Auto-Add Custom Field If Does not Exist’ is closed to new replies.