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/
Related
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?
As the question asks, I'm trying to understand one thing, as this is a popular of the fields within the table, but I can't understand their usefulness.
global $wpdb;
$wpdb->insert(
$wpdb->postmeta,
array(
'post_id' => '1',
'meta_key' => 'address',
'meta_value' => '1428 Elm St.'
),
array(
'%d',
'%s',
'%s'
)
);
One thing is the post-table table?
What is the use of this table as in the previous case, for the seo?
in my searches on google, I saw that setting the correct tags, involves a better indexing by google, and for this there is a truly spectacular tool, which allows you to have access by entering the word of the topic we want to write like post and get all the queries, that people type more on google: (https://answerthepublic.com/)
but setting tags and populating the postmeta table are not the same thing?
What is the utility of populating with a code like the postmeta table before?
Its NOT for SEO. Its for custom fields. On WP by default for posts and pages you have wp-post table, that tables has following columns (https://i.imgur.com/NCBslgq.png) for expanding functionality and adding extra custom fields WP has other table wp-postmeta (meta_id, post_id, meta_key, meta_value). So if you want add address field to post or custom post, you attaching meta field to that post using add_post_meta() https://codex.wordpress.org/Function_Reference/add_post_meta
I'm working on a plugin which is going to save about 10 custom user meta for certain users bound to the plugin. Among these metas we find: Address, zip, occupation, etc...
These metas will only be used by the plugin, and mostly (if not always) all of them will be fetched from the DB together to be shown in a table view in the admin.
So, I'm thinking about the best way to save these values.
I could do this:
add_user_meta( $user_id, 'address', 'kings street 45');
add_user_meta( $user_id, 'zip', '12345');
add_user_meta( $user_id, 'occupation', 'nurse');
... some more meta
Or would it be better to do this:
add_user_meta( $user_id, 'plugin_name_user_meta', array(
'address' => 'kings street 45'
'zip' => '12345'
'occupation' => 'nurse')
... some more meta
);
In WordPress I prefer to work with arrays because in helps keeping my data organized, so I would go the second way. Nevertheless, if you go the first way, prefix all the metas with a unique ID related to your plugin.
I disagree with the first answer and I would use your first suggestion.
Why? Because if you use add_user_meta for each field you have a seperate field in the database for each value. That means:
1) You can do meta and Wildcard Queries e.g. "Select all user with a ZIP starting with 11". This is impossible if you save an array especially as the array will be saved in serialized format.
Keep this possibility open! Some day you may want to do complicated queries on this data even if it is not the case currently.
Have a look at the WP Meta Query class and even better the WP User Query class:
https://codex.wordpress.org/Class_Reference/WP_Meta_Query
https://codex.wordpress.org/Class_Reference/WP_User_Query#Custom_Field_Parameters
2) You do not have a disadvantage in extensibility: As these fields are already saved in a meta table and not within fixed columns you can add and remove values dynamically.
However #Juan is right with the hint on prefixes: You definitly should prefix your meta_values in order to avoid collisions.
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/
I want to add two custom field to table wp_posts, and that I need for my plugin.
now to enable these fields I have changed core file of wordpress
wordpress/wp-admin/post.php
$data = compact( array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'guid' ) );
here I have added two fields which I want.
Now I want these things as installable( I have manually added two fields here).
So how to do that within plugin.
I have read one post
http://wp.tutsplus.com/tutorials/creative-coding/add-a-custom-column-in-posts-and-custom-post-types-admin-screen/
here hooks are used in theme function.php,but i want to do in plugin itself.
I am using wordpress 3.6.
still any confusion please comment I will update.
As pointed in the question comments, you should never edit wp core files(reason: they get overwritten at updates), and you should never modify wp tables (can cause crashes at updates).
You are developing a plugin, then you have a few options for the database setup:
1) you can use existing database tables
* you might use the postmeta table
2) if for any reason you can't use post meta, create your own table
* add the 2 columns that you need and a column as post id, this way things will run smoothly
PS: you can use in your plugin all wordpress functions, just search them in the codex and see how you need to use them.
also check this info about creating database tables with plugins