Automatically insert WordPress custom field - wordpress

I want to add a new custom field with a meta key and meta value to my posts.
Currently the only way it is added to a post is if I go into the post and click Update.
I have lots of posts and essentially want this custom field to be added to all posts automatically with an assigned meta value.
This meta value is different for each post.

I found this helpful: http://www.catswhocode.com/blog/wordpress-how-to-insert-data-programmatically
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);
}
}
add_action('publish_page', 'add_custom_field_automatically');
add_action('publish_post', 'add_custom_field_automatically');
This will add a 'custom value' to the 'field-name' in the $post_ID.

I think the right method is using the 'save_post'hook,such as:
function cwp_add_custom_post_meta($post_id, $post){
global $wpdb;
$post_cat_id=get_the_terms( $post_id, 'category' );
$post_cat_id=cwp_object_to_array($post_cat_id);
$post_cat_id=$post_cat_id['0'] ["term_id"];
$display_voting = get_tax_meta($post_cat_id,'cwp_display_voting');
if(!wp_is_post_revision($post_id))
update_post_meta($post_id,'display_voting', $display_voting);
}
add_action( 'save_post', 'cwp_add_custom_post_meta', 10, 2 );

Related

Can you edit WordPress custom post types directly from the archive list page?

Is there a way to edit WordPress custom post types directly from the archive list page. For example, you would use the "list" view to edit the posts and pages in that custom post type, including all o the custom fields.
Option 1: Using a plugin
If you're using ACF, the easiest way to do this would be with ACF Quick Edit Fields plugin.
Option 2: Using code
If you don't want to use a plugin, you need to:
Add columns to the list page
Get the fields and display it in these columns
You can add the following code to your theme's functions.php:
Add columns for the data you want displayed in the list
// Replace 'cpt_name' with your custom post type name
function add_custom_columns ( $columns ) {
$columns['custom_field_1'] = __( 'Custom Field 1 Column Name' );
$columns['custom_field_2'] = __( 'Custom Field 2 Column Name' );
return $columns;
}
add_filter ( 'manage_cpt_name_posts_columns', 'add_custom_columns' );
Get the fields and display it in the columns
function display_cf_columns ( $column, $post_id ) {
switch ( $column ) {
case 'custom_field_1':
echo get_post_meta ( $post_id, 'post_meta_1', true );
break;
case 'custom_field_2':
echo get_post_meta ( $post_id, 'post_meta_1', true );
break;
}
}
add_action ( 'manage_cpt_name_posts_custom_column', 'display_cf_columns', 10, 2 );

How to update Order meta data, from a WooCommerce Action

I'm trying to add some custom meta_data to a WooCommerce Order, by running a Order action.
Here is my code:
function custom_add_order_actions( $actions ){
global $theorder;
$actions['my_custom_action'] = 'My custom action';
return $actions;
}
add_action( 'woocommerce_order_actions', 'custom_add_order_actions' );
function custom_add_single_action( $order ){
// Non of these change anything on the order
$order->set_billing_first_name( 'A new test name' );
$order->update_post_meta( 'a_test_field', 'Test field value' );
update_post_meta( $order->get_id(), 'a_test_field', 'Some other value' );
// $order->save(); // I even tried adding this as well, but it doesn't change anything.
}
add_action( 'woocommerce_order_action_my_custom_action', 'custom_add_single_action' );
How do I change the order (or specifically, post_meta fields for an order) from inside an action?
A example
Imagine that I add a post_meta field, with the field name (key): a_test_field.
It's currently an ACF-field, but it's the same for regular WordPress custom fields.
If I change the value of the field and press 'Update', then the value changes:
So far so good. Now the value of the field is 'Foobar'.
What's wierd is that even if I do this:
add_action( 'woocommerce_order_action_my_custom_action', 'custom_add_single_action' );
function custom_add_single_action( $order ){
update_post_meta( $order->get_id(), 'a_test_field', 'A new value' );
die(); // This die is vital, to make the change in the database.
}
Then I can see the value change in the database to 'A new value'.
But if I just do this:
add_action( 'woocommerce_order_action_my_custom_action', 'custom_add_single_action' );
function custom_add_single_action( $order ){
$order->update_post_meta( 'a_test_field', 'A new value' );
// No die(); here...
}
Then the value remains 'Foobar' in the database.
Sorry but the following lightly revisited code works (Selecting the action and click on the button arrow):
add_action( 'woocommerce_order_actions', 'add_custom_order_action' );
function add_custom_order_action( $actions ){
$actions['my_custom_action'] = __('My custom action', 'WooCommerce');
return $actions;
}
add_action( 'woocommerce_order_action_my_custom_action', 'triggered_custom_order_action' );
function triggered_custom_order_action( $order ){
$order->update_meta_data( '_test_1_custom_field', 'AAFFBB9977' );
$order->save();
update_post_meta( $order->get_id(), '_test_2_custom_field', 'Some other value' );
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Note: Order actions are mostly used for some other things than what you are trying to do.
Now when using a meta box with an input field (as you are showing), on submit, you should save that field value using the action hook save_post_shop_order like in those related threads:
Metabox with multi checkbox in WooCommerce admin single orders
Dynamic custom order numbers based on payment method
Metabox with multiple custom fields for WooCommerce admin order pages

Dynamic permalinks when creating a post in WordPress

I have a custom post type named houses. Inside my custom post type I have several custom fields which I created using ACF.
What I need to do is to change the permalink when I create a new post.
I would like to use the code and title fields to customize the permalink:
//code + post title
4563312-house-example-1
I'm developing a plugin which controls everything.
Is there a way to intermediate the creation of a post to update its permalink?
Thanks.
After some research, I found an answer related to wp_insert_post_data.
Using wp_insert_post_data, I couldn't get the custom field value, and to achieve that, I had to use another action, save_post instead.
function rci_custom_permalink($post_id) {
$post = get_post($post_id);
if($post->post_type !== 'houses') return;
$code = get_field('code', $post_id);
$post_name = sanitize_title($post->post_title);
$permalink = $code . '-' . $post_name;
// remove the action to not enter in a loop
remove_action('save_post', 'rci_custom_permalink');
// perform the update
wp_update_post(array('ID' => $post_id, 'post_name' => $permalink));
// add the action again
add_action('save_post', 'rci_custom_permalink');
}
add_action('save_post', 'rci_custom_permalink');
PS: Since all these fields are required, I didn't need to check if they are empty or not.
For reference about save_post action:
Plugin API/Action Reference/save post

Advanced Custom Fields repeater delete_post_meta hooks passing the wrong meta

I'm trying to run some additional functions when updating and deleting ACF custom post meta, ACF version 4.3.8. The ACF field type is a repeater with several rows. When I delete one of these rows, I'm getting the wrong $meta_key passed to my hook:
<?php
class My_Consultant_Save_Post {
function __construct() {
add_action( 'delete_post_meta', array ( $this, 'delete_consultant_meta_connections'), 10, 4 );
}
public function delete_consultant_meta_connections( $meta_id, $post_id, $meta_key, $_meta_value ) {
echo 'post_id';
var_dump($post_id);
echo 'meta_key';
var_dump($meta_key);
echo 'current value of meta, before deleting';
$current = get_post_meta($post_id, $meta_key);
var_dump($current);
die;
}
}
$my_consultant_update = new My_Consultant_Save_Post();
see delete_postmeta function, and the action delete_post_meta
actually it appears to be passing the $meta_key of the last row entry I have in the field group's repeater field, and definitely not one that I deleted from the front-end post editor's acf remove row button.
Any ideas why the hook is not returning the meta_key of the actual meta I tried to delete?

update custom post custom field value when post is saved in wp-admin

How to update custom post custom field value when post is saved in admin?
I have tried to use this in misc.php for admin section:
add_action('pre_post_update', 'do_something_with_a_post');
function do_something_with_a_post($id) {
global $post;
update_post_meta($id, 'ct_Course_Dur_text_d19c', 'test12');
)
But it is not working.
You may try this (Using save_post hook), paste this code in your functions.php file
function save_cpt_metadata($id, $post)
{
if($post->post_type != 'your_custom_post_type') {
return;
}
update_post_meta($id, 'ct_Course_Dur_text_d19c', sanitize_text_field( $_POST['your_custom_field'] ) );
}
add_action('save_post', 'save_cpt_metadata');
In this example sanitize_text_field( $_POST['your_custom_field'] ) is would be actually the cstom field on your form but you may use any hard coded data, replace the your_custom_post_type with your real custom post type.

Resources