Automatically add custom fields value to wordpress post

admin2025-01-07  3

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.

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.

Share Improve this question edited Mar 2, 2018 at 9:54 juicebyah asked Mar 2, 2018 at 3:50 juicebyahjuicebyah 1114 bronze badges 4
  • if check $post_ID get ? – Tarang koradiya Commented Mar 2, 2018 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 Commented Mar 2, 2018 at 6:15
  • edited. i hope its clear enough – juicebyah Commented Mar 2, 2018 at 9:54
  • Did you add edit_page as a custom capability ? Because this one does not exist default, only edit_pages. – Beee Commented Mar 2, 2018 at 10:15
Add a comment  | 

2 Answers 2

Reset to default 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>:

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 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 );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256187a341.html

最新回复(0)