Run function after any category is deleted - wordpress

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.

Related

Woocommerce Product Vendor extension - Loading ACF fields into a vendor's store front

I am using ACF to add fields to my vendors' dashboard profile pages. I currently have a test ACF field loading the field from only the WP Admin profile page on all the vendors' product listing page using this simple hook in my child theme's functions.php:
add_action( 'woocommerce_archive_description', 'vendor_profile', 7 );
function vendor_profile() { ?>
<?php if(get_field('founded_on')) { ?>
<?php the_field('founded_on'); ?>
<?php }
}
Perhaps I'm pulling the wrong hook, but I can't seem to find the right hook in the Product Vendor frontend page.
I need help customizing it so that when you are on a vendor's product page, it pulls the ACF fields from that particular vendor's profile. Currently, it partially works; however it only pulls the WP main admin's data for all the different vendors'.
I know there is a way to pull the vendor's ID for each particular vendor's page and have it load their data for their page, but my php knowledge is very limited. I usually just hunt for existing code and tweak it. Unfortunately I haven't found any solutions that have worked, and this is the closest I've come to getting custom fields to work on a vendor's page.
Or if anyone can point me to a better solution to allow me to create customer fields for a vendor to fill out that will be loaded on their front end page, that would be great. I've tried Nicola Mustone's solution ( here ), which would have been perfect, except I couldn't get it to load the new custom fields on the vendor's store profile form page, nor have it load the fields into that vendor's storefront page. Based on comments, it only shows up for the site's Admin and only they can edit it. There's no visible way to have it load on the storefront, which defeats the purpose.
I imagine that the providers are users with a certain level within the WordPress system?
Considering that this is your case, the ACF fields need some additional parameters to become visible:
$post_id = false; // current post
$post_id = 1; // post ID = 1
$post_id = "user_2"; // user ID = 2
$post_id = "category_3"; // category term ID = 3
$post_id = "event_4"; // event (custom taxonomy) term ID = 4
$post_id = "option"; // options page
$post_id = "options"; // same as above
$value1 = the_field( 'my_field', $post_id );
$value2 = get_field( 'my_field', $post_id );
take some examples found in the ACF documentation, but in your particular case you have to pass the user's ID
the_field('founded_on', 'user_' . $user->ID );
echo get_field('founded_on', 'user_' . $user->ID );
function documentation the_field()
You need to pull the user ID of the user and then use the format user_{$user->ID} for the post ID as the second parameter of the ACF field.
If I understand your question, then this should work.
add_action( 'woocommerce_archive_description', 'vendor_profile', 7 );
function vendor_profile() {
$user = wp_get_current_user();
if ( get_field( 'founded_on', 'user_' . $user->ID ) ) {
the_field( 'founded_on', 'user_' . $user->ID );
}
}

How to display a custom field in the dashboard posts list( replacing the title )

I'm using WordPress and I did create a custom post type with several custom fields ( using ACV, advanced custom fields )
I have hidden all the basic WordPress fields like title, content editor thumbnails, etc, etc so I leave just my custom fields for the creation of a new post.
Since the title is not filled at the creation of the post, I get a list of post with every title set as “auto draft”
The thing that I don't want obviously.
My question is simple :
Is it possible and if yes how to replace the title with one of my custom fields in the dashboard post list.
I searched everywhere but I couldn't find an answer.
Sorry for my English, it’s not my native tongue, I hope You understand what I basically want to do.
Thanks for your time to read my question.
Have a good day
I managed to find a solution myself.
Let us say that you have a custom post type named "test" with 3 custom fields named one, two and three. And you want to remove the title and the date, show the content of one, two, and three in the post list table.
First you have to create a function that removes the title and the date, and also creates the new columns.
function custom_columns($columns)
{
unset($columns['title']);
unset($columns['date']);
return array_merge(
$columns,
array(
'one' => __('One'),
'two' => __('Two'),
'three' => __('Three')
)
);
}
add_filter('manage_test_posts_columns', 'custom_columns');
Then you need to display the custom field content in the post list table:
function display_custom_columns($column, $post_id)
{
switch ($column) {
case 'one':
echo get_post_meta($post_id, 'one', true);
break;
case 'two':
echo get_post_meta($post_id, 'two', true);
break;
case 'three':
echo get_post_meta($post_id, 'three', true);
break;
}
}
add_action('manage_test_posts_custom_column', 'display_custom_columns', 10, 2);
Further reading
You can use wp_insert_post_data to accomplish the desired functionality. This action is triggered when a post is created or updated.
add_action( 'wp_insert_post_data', 'my_updated_title', 99, 1 );
function my_updated_title( $data ) {
// If this is just a revision, don't update title.
if ( wp_is_post_revision( $data['ID'] ) ) {
return;
}
// Set the post title to your custom field value
$data['post_title'] = get_field( 'your_acf_field', $data['ID'] );
// Return the updated post data
return $data;
}

Woocommerce /Display Custom Field in Wordpress

I added some custom fields to the each product in Woocommerce, and I would like the data from the custom fields to show on the following receipt page (Order Details) after checkout is completed.
Just can't get it, not strong in php, why can't get anything. I've tried to use var_dump(get_post_custom($order->id)); i don't have my custom field in result.
Can somebody light me up?
Here is my code:
add_action( 'woocommerce_order_details_after_order_table', 'code_activation', 10, 1);
function code_activation($order){
echo '<p><strong>'.__('Activation code').':</strong> ' . get_post_meta( $order->id, 'activation_code', true ). '</p>';
}
You have added custom fields to the product. So custom fields will be associated with your product-id .
But you are trying to retrieve the custom fields using $order->id which is wrong. following code should help to retrieve the product id from order. And using the product id you can retrieve your custom field.
$orderItems = $order->get_items();
foreach($orderItems as $orderItem)
{
$product_id = $orderItem['variation_id'] ? $orderItem['variation_id'] : $orderItem['product_id'] );
}

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?

wordpress advanced custom fields select values

I'm working with Wordpress 3.5.2 and plugin Advanced CUstom Fields 4.1.8
I have a group of fields (called "P") and some fields.
I want to retreive all data from a specific select field, and I found some code in the plugin documentation:
$values = get_field('field_519a0279bc93e');
if($values)
{
foreach($values as $value)
{
echo '<li>' . $value . '</li>';
}
echo '</ul>';
}
In $values, getting two different select fields In a case I get a string and in another a boolean.
I supposed is so simple, but I can't find the solution.
Thanks in advance.
It is important to understand that when using the Advanced Custom Fields plugin all this does is create an easier method for admins to add custom fields for posts, so the WordPress get_post_meta() still applies.
Say you've created a custom field called 'sex' and you've assigned a dropdown or radio buttons to it and you wanted to display what this value in the post or the loop, you would do the following.
Outside the loop
## Returns sex custom field value for current page ID
$sex = get_post_meta( get_the_ID, 'sex', true);
Inside the loop
## Returns sex custom field value for current loop iteration
global $post;
$sex = get_post_meta( $post->ID, 'sex', true);

Resources