Advanced Custom Fields repeater delete_post_meta hooks passing the wrong meta - wordpress

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?

Related

Run function after any category is deleted

I need to delete some of the values of custom field whenever any categories is deleted. I tried this hook but it is not working.
add_action( 'delete_term_taxonomy', function($tt_id) {
// Whatever i need to do after a category is deleted
}, 9, 1);
Can any one help me with this?
EDITED:
I am dynamically populating list of categories into advanced custom field repeater which has fields like category_name, category_description, category_slug, enabled and order.
function my_acf_set_repeater( $value, $post_id, $field ){
// echo '<pre>'; print_r($value); echo '</pre>';
// this one should consists array of the names
$settings_values = get_categories();
$i = 0;
foreach( $settings_values as $settings_value ){
// echo $settings_value->name;
$value[$i]['field_6357b02a9bb1b'] = $settings_value->name;
$value[$i]['field_63594327553ef'] = $settings_value->description;
$value[$i]['field_635945b6aa743'] = $settings_value->slug;
$i++;
}
// echo '<pre>'; print_r($value); echo '</pre>';
return $value;
}
add_filter('acf/load_value/name=categories_to_display_in_homepage', 'my_acf_set_repeater', 10, 3);
This repeater is shown in theme option page, so that user can enable which categories to show in home page along with ordering it.
Fields like category_name, category_description, category_slug are dynamic. Fields enabled and order are custom which user fills in.
Its working fine. But the problem here is, whenever a category is deleted, it is removed from theme option page as well. But the enabled field and order field values are shifted to another category in that repeater field.
So I need to remove that field values related to deleted category as well.

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

Unable to save Query Vars with a post

I'm trying to save Query Vars to a post for later retrieval.
I'm using permalinks in this format: domain.com/%category%/%postname%/
Example:
I create a following page
domain.com/page-003/
I add Query Var called email to the page
add_query_arg('email', 'test#abc.com', 'domain.com/page-003/')
Now when I call
get_permalink($post_id);
I get
domain.com/page-003/
Instead of
domain.com/page-003/?email=test#abc.com
What am I missing? Aren't Query Vars saved with a post?
You want to save some meta data which you want to restore later on and add as query arg into the URL.
You need to first save it as post_meta like e.g. when you save post with that data. You use:
<?php update_post_meta(get_the_ID(), 'email_address', 'abc#mail.com'); ?>
More details: https://codex.wordpress.org/Function_Reference/update_post_meta
Then during the retrieval, you may hook into a HOOK early on like template_redirect or earlier of the post you can get post_meta to get the email and then add to query arg:
<?php $email = get_post_meta( get_the_ID(), 'email_address' ); ?>
Then
esc_url( add_query_arg( 'email', $email, get_permalink( get_the_ID() ) ) );
Something like that, code is untested, I just wrote here, you may please read detailed doc in codex for each function used above.
Update: How to update/fill Ninja form field from Meta Value:
add_filter( 'ninja_forms_render_default_value', 'wm_the_value' , 10 , 3);
function wm_the_value( $default_value, $field_type, $field_settings ) {
if( 'textbox' == $field_type && in_array('ref' , $field_settings)){
$default_value = get_post_meta(get_the_ID(),'_listing_mls', true);
}
return $default_value;
}
'ref' is field name in Ninja form.
'_listing_mls' is meta_key name from WP database.
Hope it works for you.

wp_update_post make custom field values disappear (Wordpress)

I'm trying to update the post content in one of my post through the wp_update_post function. I have read the documentation here: http://codex.wordpress.org/Function_Reference/wp_update_post
And if I get it right I just need to send the post ID and the post content I want to update with - just like in the example - and this should be the only thing that will change. Although my custom fields that I have attached to this post disappears, strange enough.
I have the following code that I pass on:
if(isset($_POST['submit'])){
$the_post = array();
$the_post['ID'] = $_POST['id'];
$the_post['post_content'] = $_POST['recension'];
// Update the post into the database
wp_update_post( $the_post );
}
How come this happen and how do I solve it?
This is because when you are updating the post the *wp_insert_post* function is used and there is "save_post"(1) action hook which is usually used for saving custom fields data.
The standard way to add/update post meta is something like this:
$post_meta['_mymeta'] = $_POST['_mymeta'];
// Add values of $events_meta as custom fields
foreach ($events_meta as $key => $value) { // Cycle through the $post_meta array!
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
if($value && $value != get_post_meta($post->ID, $key, TRUE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} elseif($value && get_post_meta($post_id, $key, TRUE) == "") { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value, TRUE);
}
if(!$value) delete_post_meta($post->ID, $key, get_post_meta($post->ID, $key, TRUE)); // Delete if blank
}
...as you can see it is checking for *$_POST* data and if it is empty or not set it updates your meta value with empty data or deletes it completely.
I suppose you should use database update function or some other API function to update post fields...for example this piece of code will update your post menu order:
$wpdb->update( $wpdb->posts, array( 'menu_order' => 5 ), array( 'ID' => $post->ID ) );
(1) Runs whenever a post or page is created or updated, which could be from an import, post/page edit form, xmlrpc, or post by email. Action function arguments: post ID.
To avoid that behavior set false as third parameter.
It deactivates the "after insert hooks".

Automatically insert WordPress custom field

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

Resources