How to copy data over into my custom database table - wordpress

I have the following function which inserts data from the postmeta table into a custom database table wp_fixtures_results.
I am using WPAll Import Plugin action pmxi_saved_post. So the code runs during an import process.
The purpose of the code is to migrate data from wp_postmeta into wp_fixtures_results which is the custom table.
When running the code for a fresh import, the data is stored which is ordinarily stored in wp_postmeta is the moved into the custom table. This works perfectly.
However, the data only runs for the INSERT query as shown in the code. Using the same plugin action I need to update the data from postmeta into the custom table. The issue is the code is only working for the INSERT query. How do I check if the data has changed in the postmeta and during the import process which updates the data, update the custom table too?
if ($post_type === 'fixture-result') {
function save_fr_data_to_custom_database_table($post_id)
{
// Make wpdb object available.
global $wpdb;
// Retrieve value to save.
$value = get_post_meta($post_id, 'fixtures_results', true);
// Define target database table.
$table_name = $wpdb->prefix . "fixtures_results";
// Insert value into database table.
$wpdb->insert($table_name, array('ID' => $post_id, 'fixtures_results' => $value), array('%d', '%s'));
// Update query not working - doesn't change data.
$wpdb->update($table_name, array('ID' => $post_id, 'fixtures_results' => $value), array('%d', '%s'));
// Delete temporary custom field.
delete_post_meta($post_id, 'fixtures_results');
}
add_action('pmxi_saved_post', 'save_fr_data_to_custom_database_table', 10, 1);
}
The wp_postmeta table
The wp_fixtures_results (custom table)

This is an old question by now, but I'd say that the $wpdb->update is redundant. The insert is going to create the row and store the fields. The $wpdb->insert should return "1" - check that value to verify that the insert succeeded before deleting the postmeta entry. The call to update is not necessary.

Related

wordpress query builder (typerocket) get post with relationship table data

I'm trying to get some post 'wp_posts.post_type', 'servitod_plan' in this case there are only 2 plans with that post_type. these posts have associated some wp_postmeta . when I try to bring each post, with the data of its relationship with postmeta brings me duplicates. when I look at the record it brings me
one object for each wp_postmeta.meta_key
I only want to bring the posts that correspond to 'wp_posts.post_type', 'servitod_plan' and their relation wp_postmeta of key name: value in this case plan_fee: 3000, etc.
My Controller.
$query = tr_query()->table('wp_posts');
$query->setIdColumn('ID');
$results = $query->select('wp_posts.post_name', 'wp_posts.ID', 'wp_postmeta.meta_value')
->join('wp_postmeta', 'wp_postmeta.post_id', '=', 'wp_posts.ID')
->where('wp_posts.post_type', 'servitod_plan')
->get();
return $results; // for develpment and test
MY WP_POSTMETA TABLE
MY WP_POSTS TABLE
when i see the RETURN DATA $RESULTS
You can just pass one more ->where('wp_posts.ID', 2271) and you can pass post id. check the below code.
$query = tr_query()->table('wp_posts');
$query->setIdColumn('ID');
$results = $query->select('wp_posts.post_name', 'wp_posts.ID', 'wp_postmeta.meta_value')
->join('wp_postmeta', 'wp_postmeta.post_id', '=', 'wp_posts.ID')
->where('wp_posts.post_type', 'servitod_plan')->whereIn('wp_posts.ID', 2271)
->get();
return $results; // for develpment and test
Update
for multiple ids you can use like ->whereIn('wp_posts.ID', array('2271','2269' ) )

how to insert checkout page custom field value wp_posts table in wp

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.

Fetch data From Wordpress Database

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/

WordPress: get all meta data when user registers

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.

How to get last inserted row ID from WordPress database?

My WordPress plugin has a table with a AUTO_INCREMENT primary key field called ID. When a new row is inserted into the table, I'd like to get the ID value of the insertion.
The feature is to using AJAX to post data to server to insert into DB. The new row ID is returned in the AJAX response to update client status. It is possible that multiple clients are posting data to server at the same time. So, I have to make sure that each AJAX request get the EXACT new row ID in response.
In PHP, there is a method called mysql_insert_id for this feature.But, it is valid for race condition only if the argument is link_identifier of last operation. My operation with database is on $wpdb. How to extract the link_identifier from $wpdb to make sure mysql_insert_id work? Is there any other way to get the last-inserted-row id from $wpdb?
Thanks.
Straight after the $wpdb->insert() that does the insert, do this:
$lastid = $wpdb->insert_id;
More information about how to do things the WordPress way can be found in the WordPress codex. The details above were found here on the wpdb class page
This is how I did it, in my code
...
global $wpdb;
$query = "INSERT INTO... VALUES(...)" ;
$wpdb->query(
$wpdb->prepare($query)
);
return $wpdb->insert_id;
...
More Class Variables
I needed to get the last id way after inserting it, so
$lastid = $wpdb->insert_id;
Was not an option.
Did the follow:
global $wpdb;
$id = $wpdb->get_var( 'SELECT id FROM ' . $wpdb->prefix . 'table' . ' ORDER BY id DESC LIMIT 1');
Get the last inserted row id in WP like this.
global $wpdb;
$order = ['product_name'=>'Computer', 'os_system'=>'Linux'];
$wpdb->insert('wp_product_orders', $order );
$last_inserted_id = $wpdb->insert_id;
Something like this should do it too :
$last = $wpdb->get_row("SHOW TABLE STATUS LIKE 'table_name'");
$lastid = $last->Auto_increment;
just like this :
global $wpdb;
$table_name='lorem_ipsum';
$results = $wpdb->get_results("SELECT * FROM $table_name ORDER BY ID DESC LIMIT 1");
print_r($results[0]->id);
simply your selecting all the rows then order them DESC by id , and displaying only the first
Putting the call to mysql_insert_id() inside a transaction, should do it:
mysql_query('BEGIN');
// Whatever code that does the insert here.
$id = mysql_insert_id();
mysql_query('COMMIT');
// Stuff with $id.

Resources