I am using listify theme and wp job manger field editor addon i am adding one new field and that meta_key field is (_get_the_author_meta) Now data store in database i am fetch data in fornt end Please send me full code how to fetch data through meta_key.
You can use this function to get meta value.
get_post_meta( int $post_id, string $key = '', bool $single = false )
For more details visit this page.
https://developer.wordpress.org/reference/functions/get_post_meta/
Related
I'm trying to access some fields that have been assigned to a post via a plugin named Snax.
You can see here in the admin that "Story" has being assigned to a post as a "Snax Format":
But when I check the $post object inside the loop on the front end using something like $my_post = get_post(); var_dump($my_post); I can't see any anything relating to snax or "story".
What php can I use to access that post data on the front end inside the loop?
I'm asking because I want to use that field data inside my post template.
I think the plugin will store they data as meta_values. You can retrieve this data with get_post_meta()
$meta_data = get_post_meta( get_the_ID() );
var_dump( $meta_data );
I have added the custom field check out page. I want to add this custom field's value to the order list in the wp_posts table
Here I have added a Row delivery_option in wp_posts table
http://prntscr.com/rbdjcy
Thanks
in WP you dont need/shouldn't change wp tables are this will be overwritten in the next update/upgrade. To save those values use WP php functions (forget database as WP functions will handle saving/getting for you from meta table)
So to add or update use
update_post_meta($post_id, $meta_key, $meta_val);
here meta key will be "delivery_option" and meta_value will be anything you want to store. $post_id will be current post/product id , you can get as get_the_ID(); Also if you do not want this custom field show up in WP default custom fields metaboxes, you may prefix your meta key with "_" underscore so "delivery_option" will be become "_delivery_option"
Then to get this data back :
$delivery_val = get_post_meta($post_id, 'delivery_option', true);
or
$delivery_val = get_post_meta($post_id, '_delivery_option', true); //_used to keep it hidden from WP default custom fields metaboxes
Source:
https://developer.wordpress.org/reference/functions/get_post_meta/
https://developer.wordpress.org/reference/functions/update_post_meta/
https://developer.wordpress.org/reference/functions/add_post_meta/
PS. Even there is add_post_meta , normally we use update_post_meta as it will add if no value exists or will update if any old value exists.
I am using the following code to solve this but this is not working for orders from guest customers . However this is working fine for the orders belonging to some registered user/customer but not for the orders belonging to guest customers.
Solution credit to LoicTheAztec for answer
function cristmas_bulk_editing_orders(){
if(!is_admin()) return; // Will work only from Admin Backed.
else {
$order_id = 9458;
$new_customer_id = 479;
// Getting the postmeta customer ID for 'order' post-type
$customer_id = get_post_meta( $order_id, '_customer_user', true );
var_dump($customer_id);
// If it's an existing order and doesn't have already this user ID
// It update the customer ID
if( !empty($customer_id) && $new_customer_id != $customer_id )
update_post_meta($order_id, '_customer_user', $new_customer_id,0);
echo 'order updated';
}
}
cristmas_bulk_editing_orders();
ORIGINAL ISSUE
We imported the orders via woocommerce order export & import plugin from woocommerce team ..
But in the process something went wrong.. Most of the orders were not assigned any customer ..
So now when ever a new customer registers he/she is assigned 1 of these orders automatically ..
So basicallly all of them see 1 order in their recent orders which belongs to some other guest cusotmer , then they have all the information about other customer . their email etc..
So one option is I find out all the orders(with issues that is no customer assisned to them ) and I assign them to admin ..but this also have some issuses.....
SO is there any other option that these new registered users don't get old orders assigned..
Please help
May be you could try this other similar function based on SQL queries, that should do the trick. Before starting, make a database backup. I have add controls (or limitations) like :
A max date (in YYYY-MM-DD 00:00:00 date time format)
Order number range
So here is that code:
function easter_bulk_editing_orders(){
if( ! is_admin() ) return; // Will work only from Admin Backed.
else {
$processed_orders = array();
// Define the replacement user ID
$replacement_user_id = '2500';
// Define the DATE LIMIT for guest orders max date limit
$max_date = '2017-12-09 00:00:00';
// Define an order range
$order_id_min = '0';
$order_id_max = '100000';
global $wpdb;
// Get all guest orders below a defined max date
$old_guest_orders = $wpdb->get_results( "
SELECT pm.*, p.post_date
FROM {$wpdb->prefix}postmeta AS pm
LEFT JOIN {$wpdb->prefix}posts AS p ON pm.post_id = p.ID
WHERE pm.post_id BETWEEN $order_id_min AND $order_id_max
AND pm.meta_key LIKE '_customer_user'
AND ( pm.meta_value LIKE '0' OR pm.meta_value LIKE '' )
AND p.post_date <= '$max_date'
" );
foreach( $old_guest_orders as $guest_order ){
$meta_id = $guest_order->meta_id;
$wpdb->query( "
UPDATE {$wpdb->prefix}postmeta as pm
SET pm.meta_value = '$replacement_user_id'
WHERE pm.meta_id = '$meta_id'
" );
// Set each order ID in an array
$processed_orders[] = $guest_order->post_id;
}
// Testing ( raw output of processed orders IDS )
var_dump($processed_orders);
}
}
// Run the function
easter_bulk_editing_orders();
Code goes in function.php file of your active child theme (active theme or in any plugin file).
You have to use this function only once and to remove it afterwards (see below).
USAGE:
Once this code is pasted and saved on function.php file, display or reload any Admin page from backend within your browser.
Now you can comment the function this way and save:
// easter_bulk_editing_orders();
Check that the orders have been changed as you want, and remove all this code.
Code is tested and works.
There is error in your importing so try to re-import. Have you tried WP All Import Premium. It will import anything with drag and drop titles, categories, and meta fields. you can also assign new meta name to post meta while importing. If you need more help then share more details.
i am using wordpress database as a backend for android application and i have 2 custom roles in the application one is management and second is user.and i am using members plugin for content permission in wordpress which allows to add permissions.now from my plugin i want to get all the posts for users which has management role.these request will be come as a REST based URL and return json data.
try this
create a function to alter the query's where clause:
function authors_where_filter( $where ) {
global $wpdb;
$ids = get_users(array('role' => 'author' ,'fields' => 'ID'));
$where .= " AND post_author IN ($ids)";
return $where;
}
and then before you query just hook it ex:
add_filter('posts_where','authors_where_filter');
$all_posts = new WP_Query(array('posts_per_page' => -1 .....
remove_filter('posts_where');
and you should get all posts of author users in a single query, (well two actually one to get the users and the other is to get the posts)
Source
I have a user registration form in the front end (in the Users admin section as well) with three extra fields (apart from default ones): birthday, country, language. their values are stored in usermeta table.
I have this action hook to retireve all meta data for the registered user:
add_action('user_register', 'new_user_func');
// user registration callback function
function new_user_func($userID) {
$newUser = get_user_meta( $userID );
$userMeta = array();
foreach ($newUser as $key => $value) {
$userMeta[$key] = $value[0];
}
//do something with $userMeta...
}
var_dump($userMeta) after submit doesn't give me the extra fields value though.. only defaults (first name, last name etc)
Anyone know what might be the case?
Did you try getting the values with:
$meta = get_the_author_meta($meta_key, $user_id);
Perhaps the meta values you add yourself isn't supported by get_user_meta() .
If this don't work either, perhaps you need to look on how you went about creating the new meta fields. Theres a pretty decent tutorial on how to do it here:
http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields
Read de Codex entry for user_register action, it says:
Not all user metadata has been stored in the database when this action is triggered.