Do not update specific custom field on save_post in WordPress - wordpress

As several fields are updated programmatically, I want those not to be updated when a post is saved or updated inside the wp-admin dashboard.
For example, the ACF repeater field named tickets must not be saved during backend editing and when the post is saved or updated (because entries that are added programmatically during the time the post is edited are deleted when saved).
My idea: Before a post is updated, save values of tickets right before that post is saved using acf/save_post before save and then update_field() using acf/save_post again after save.
I know that you rightfully ask for the own approaches and attempts at solutions. Unfortunately, I don't have any. I have no idea how to combine these two acf/save_post correctly. If this is the right approach at all. That's why I would be all the more grateful for your help and support.
Thank you so much!

I had a scenario similar to this where I didn't want users to be able to manually update fields. I created a function that would let me use an ACF filter to disable the specific fields. It looked like this:
function vgs_set_acf_fields_to_disabled( $field ) {
global $post;
if ( isset( $post ) ) {
$field['disabled'] = 1;
}
return $field;
}
add_filter( 'acf/load_field/name=tickets', 'vgs_set_acf_fields_to_disabled' );
Then, if you have more fields, you can add more add_filter lines and simply update the name variable to match your ACF field name.
Maybe this will work for you as well?

Related

ACF unable to use user field

I am totally new to wordpress and acf and I'm trying to use a user field in a post to show the author of the post. I tried to programatically add the post author id as a meta attribute in the wp_postmeta table like this:
update_field('video_author', get_current_user_id(), $post_id);
The post author, however, is not shown:
What am I doing wrong?
Also when I click the user field no users are listed. I presume acf looks for users in either "wp_users" or "users" table, both of which contain records. However, this is shown:
Thank you in advance.
When you inspect element your field , you will find key like field_59c383916102a
update_field( 'field_59c383916102a', get_current_user_id(), $post_id );
//Replace field_59c383916102a with your key value

How to create a custom meta table in wordpress?

I have custom meta box with multiple fields and it is working fine. Now, I want to store this meta box data into a custom table. So how can I do that ?
I have researched on google and Youtube but didn't got what exactly I am looking for.
If anyone can provide me with Step by Step guide or tutorial links then it will be very much helpful.
I'm not entirely sure why you would want to store Post Meta into a separate table, but I've had situations where I've needed to do crazier things.
The gist would be that you can use your Custom Meta Box to display the form fields, and then handle those form fields with your own custom function on the save_post hook.
Let's say you've registered a custom meta box with <input name="my_custom_table_field" /> in it. Instead of using update_post_meta() on the save_post hooks, you could write a function that manages the data with the $wpdb->update method. Something like this would get you started:
add_action( 'save_post', 'save_my_custom_data' );
function save_my_custom_data( $post_id ){
global $wpdb;
if( isset( $_POST['my_custom_table_field'] ){
$result = $wpdb->update(
'my_custom_table',
array(
'post_id' => $post_id,
'my_custom_table_field' => $_POST['my_custom_table_field'],
),
array(
'post_id' => $post_id
),
array(
'%d',
'%s'
),
array (
'%d'
)
);
});
}
You'll want to make sure to handle the data appropriately before saving it, of course. And again, I'm not sure why you'd need a custom "meta" table, but farbeit from me to say you "shouldn't" (especially depending on your particular usecase) - but something like the above would get you started.
To summarize:
Display your custom meta box with WP's metabox functions
Handle the save_post hook for your custom fields separately
Sanitize, trim, or otherwise make sure the data being stored is supposed to be stored in accordance to best practices for the field type
Make use of the global $wpdb class to update it.
Also of note, this answer doesn't go into CREATING the database table - because that depends on your storage engine, particular indexing needs, etc. But in general you should be able to search for "create database table in [whatever storage language]" to get a walkthrough of creating the table - then use the outline above to store the data in it.
This is the official Wordpress guide: https://developer.wordpress.org/plugins/metadata/custom-meta-boxes/

WooCommerce: Limit the fields returned in the "Update a Product" API

Is it possible to limit the fields returned in a WooCommerce POST "Update Multiple Products"? By default the API returns all the fields from each product updated. I'd like to reduce the size of the JSON returned.
The API documentation states "You may limit the fields returned in the response using the fields parameter". However, the example is for GET /wc-api/v3/products
I need to limit the fields for POST /wc-api/v3/products/bulk
I've tried appending the fields parameter to the URL, but it doesn't work (the parameter is ignored and all product fields are returned).
My URL looks like the following:
https://www.mywoocommercestore.com/wc-api/v3/products/bulk?fields=id,price,regular_price,sale_price,stock_quantity,error
If you have access to the site where WooCommerce is installed then you can make use of woocommerce_api_products_bulk_response filter and modify the output.
Add the following code to theme's functions.php file
add_filter( 'woocommerce_api_products_bulk_response', 'custom_woocommerce_api_products_bulk_response' );
function custom_woocommerce_api_products_bulk_response( $products ) {
// $products is an array containing all the data about the products
// that were created or updated. Write your logic to remove unwanted fields
return $products;
}

Woocommerce, Order Meta with Email

I can't figure out with this problem: when woocommerce sends the order processing mail to custumer, customer's meta properties are valorized in the $order object, and the valorization is done by the execution of the action woocommerce_email_order_meta (at least, I suppose):
do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text );
All fine with that, but when I want to create a custom template for that email, no meta values will be displayed in the result, although the same invokation.
I've checked the parameter values passed to the "woocommerce_email_order_meta" invokation (that routes to the order_meta() function in plugins/woocommerce/includes/class-wc-emails.php) of my costum template, and those values are the same.
Neither debugging the order_meta() function in both cases gave me clues, the function's behavior is always the same, but the result in the mail is different. So... what can I do for insert order meta info in this mail?
Sorry if this question is a duplicate and for my bad english.
Thanks in advance if you can help me with this issue.
Billing address and other details, such as first name and last name are stored as order details (In "postmeta" table). Whereas, Products in the order, its quantity, price is stored as Order meta.
Therefore, "woocommerce_email_order_meta" action will display order meta details.
Please make sure, you have included the below line, in your custom email template to get Billing address and other details.
<?php wc_get_template( 'emails/email-addresses.php', array( 'order' => $order ) ); ?>
For more information regarding, Email template customization, you can refer our blog ,
http://wisdmlabs.com/blog/customize-woocommerce-order-emails/

WP_Insert_Post change status depending on previous submissions

I have created a frontend form that allows logged in users to submit content to my wordpress site. The form allows them to submit to a custom content type. I would like be able to change the status of the submitted post depending on that users previous submissions. So if they already have 1 or more published posts then the submission goes to publish - if they don't then it goes to pending.
I know the argument that I need to change: 'post_status' => 'publish' - I just don't really have a clue how to start on the logic - let alone what the most efficient way of achieving it will be...?
first, set post_status as a PHP variable that is set by a function, and hard coded returned from within the function.
Then, refactor the function to do a separate WP_Query to find the count of published custom post types with the current userid as author. If >=1, return 'publish' else return 'draft'

Resources