0

i want to auto add custom field value when post publish , my custom fields name is "quality" and the value is "HD" .

i've look through this site and apparently codes i found is not working. so far i've tried this code :

/* Do something with the data entered */
add_action( 'save_post', 'myplugin_save_postdata' );
function myplugin_save_postdata( $post_id ) {
  if ( 'page' == $_POST['post_type'] ) {
    if ( ! current_user_can( 'edit_page', $post_id ) )
        return;
  } else {
    if ( ! current_user_can( 'edit_post', $post_id ) )
        return;
  }

  $mydata = 'quality'; // Do something with $mydata 

  update_post_meta( $post_id, 'HD', $mydata );
}

and also this code

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, 'quality', 'HD', true);
    }
}

follow instructions and put that code above on my functions.php , and nothing happend. custom fields still empty , and i look the post on homepage and no value added.

thank you! i hope my question is clear enough to understand.

  • if check $post_ID get ? – Tarang koradiya Mar 2 '18 at 5:49
  • "it is not working" just never happens with software. You need to edit the question and be much more specific on what do not happen, and what happens instead. – Mark Kaplun Mar 2 '18 at 6:15
  • edited. i hope its clear enough – juicebyah Mar 2 '18 at 9:54
  • Did you add edit_page as a custom capability ? Because this one does not exist default, only edit_pages. – Beee Mar 2 '18 at 10:15
0

I think update_post_meta( $post_id, 'HD', $mydata ); has its arguments in the wrong order and should be update_post_meta( $post_id, 'quality', 'HD');

Alternatively where you need the custom field and it does not exist, why not just use "HD" as a default value? e.g.

global $post;
$theQuality = empty(get_post_meta( $post->ID, 'quality') ? 'HD' : get_post_meta( $post->ID, 'quality', true); 

Or, if you can't get it to work on save, then try this in functions.php:

function my_add_default_cf() {
  global $post;
  if ( empty(get_post_meta( $post->ID, 'quality', true)) ) {
  // (if necessary) add additional conditional to limit which posts/pages are checked
    update_post_meta( $post->ID, 'quality', 'HD',true);
  }
}
add_action( 'pre_get_posts', 'my_add_default_cf' );

In theory inefficient as executed on every post but reality miniscule processing and unnoticeable.

Edit

I've removed the check for whether published (get_post_status( $post->ID ) == 'publish') from above code (my bad assumption).

Tested and works for me with WP 4.9.4. If a post/page does not already have cust field "quality" or it has no value, then it is added as "HD" in the following circumstances: saving of draft or on publication; on opening existing post/page for edit; preview (although done, editor page won't show it until refreshed); and when a published post with no, or empty, "quality" is visited.

If you don't want this to retrospectively apply to previously published posts then change if ( empty(get_post_meta( $post->ID, 'quality', true)) ) { to

if (get_post_status( $post->ID ) == 'draft' &&  empty(get_post_meta( $post->ID, 'quality', true)) ) {

Additional testing/diagnostics (If needed, temporarily add this to functions.php):

function my_custfield_test($post_id) {
  global $post;
    ?>
  <meta name="mypostid" content="<?php echo $post->ID; ?>" />
  <meta name="mypoststatus" content="<?php echo get_post_status( $post->ID ); ?>" />
  <meta name="myquality" content="<?php echo get_post_meta( $post->ID,'quality', true); ?>" />
    <?php  
}
add_action( 'wp_head', 'my_custfield_test' );

Then view the HTML source of a live or preview page and somewhere in <head>:

HTML source

Your original code I'm not familiar with save_post action maybe $post_id is not set? Within your 'myplugin_save_postdata' function: Try adding global $post; as the first line; and replace all occurences of $post_id by $post->ID.

  • ok , so i tried your snipped and i got error Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) – juicebyah Mar 2 '18 at 14:14
  • Answer corrected. Sorry, error due to accidentally removed $ from one of $post->ID's in if statement. on first edit. Also noticed and fixed 2 other errors in answer. My brain is having a bad day. – scytale Mar 2 '18 at 16:40
  • thank you sir , but still no value in my custom fields either on post or homepages. – juicebyah Mar 3 '18 at 3:34
  • My original code would only apply when post published. I've changed it to occur on any "save" - tested and works for me. I've also added a suggestion for your original code. – scytale Mar 3 '18 at 12:09
  • Did you add edit_page as a custom capability ? Because this one does not exist default, only edit_pages – Beee Mar 3 '18 at 12:25
0

ok so here is the working snippets i use now and verify it work

function mfields_set_default_object_terms( $post_id, $post ) {
    if ( 'publish' === $post->post_status ) {
        $defaults = array(
            'post_tag' => array( 'taco', 'banana' ),
            'monkey-faces' => array( 'see-no-evil' ),
            );
        $taxonomies = get_object_taxonomies( $post->post_type );
        foreach ( (array) $taxonomies as $taxonomy ) {
            $terms = wp_get_post_terms( $post_id, $taxonomy );
            if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
                wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
            }
        }
    }
}
add_action( 'save_post', 'mfields_set_default_object_terms', 100, 2 );

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.