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.
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 );