How to find field info in Wordpress postmeta table - php

Wordpress postmeta table has rows with meta_value field_RANDOM_STRING. How do I find field info via database?
Sample:
field_5617ad2082500
field_56d6bddfc36f7
field_5617b3adf5c19
I want to extract info about this field directly via database without going through WordPress / Wordpress Code.
I guess this has something to do with custom field types but I am not sure.
I am migrating the system out of wordpress so wanted help in extracting data.

You can use such code
global $wpdb;
$fields = $wpdb->get_results("SELECT meta_key FROM $wpdb->postmeta WHERE meta_value = 'field_58756ba2297d4' LIMIT 1");
$meta_key = ltrim($fields[0]->meta_key,'_');
$meta_value = get_post_meta($post_id, $meta_key, true);
First you get row from database where you will get meta_key for this value, then you should trim if from _ in the begining of string, because its key for field id, and then you can get actual meta value using prepeared meta_key name;

Related

display wp_postmeta meta_value in wordpress woocommerce

please help a new member
THE ISSUE:
I have a POS on my WC and it doesn't display sequential orders numbering, so I am using other plugin to achieve this. I need to display the value of a meta_value from my database in the order receipt (printable ticket) after the order is placed. So, the meta_value I need to display on the POS's tickets is the sequential ticket number created by the other plugin.
Gladly this other plugin creates the sequential number in the database from where I hope I can retrieve it somehow.
This meta_value for example "T00001...T00009" has the meta_key = "_order_number" and it's within wp5o_postmeta table of my sql database.
I believe it must be easy for you guys...
Thank you so much
Image of the data
use this filter and implement your post meta
wordpress/wp-content/plugins/woocommerce-openpos/includes/front/Front.php
line 4683
$result['data'] = apply_filters('op_get_next_order_number_info',$order_number_info);

Function to edit data on "comment_count" column on WordPress "wp_posts" DB table

What function should I use to edit content of comment_count column on wp_posts table? Example: when updating a post (with WP All Import), I want set the post's comment_count to any desired number.
Currently, WP All Import custom file uses its offer_thumbs_down data field to:
update_post_meta( $post_id, 'offer_thumbs_down', $data['offer_thumbs_down'] );
I want send offer_thumbs_down value to wp_posts > comment_count instead.
The purpose is to use comment_count as orderby
('orderby' => 'comment_count') because sorting huge number of posts by meta or custom fields is being a very slow SQL query. Using columns from wp_post table (post_date, post_title, comment_count, etc) has much better performance for orderby. Native comments are deactivated and useless for me, so I can use that field to hold other data and use it to orderby. I saw WordPress function wp_update_comment_count_now( $post_id ); but this is not for inserting data, is just to make WP count comments byitself.

Get advanced custom fields repeater field value based off of another repeater value

I have a repeater field on a page, in that field i have a post object field and a few text fields. The post object field just gets the ID of whatever post you set.
When i am a single post i want to run a database query that uses the current post ID to find the matching sub field, then get another sub field in that same repeater field.
Can anyone help with this?
This is what i have so far which is not want i need but its the closes it have been able to get
$games_id_array = $wpdb->get_results(
$wpdb->prepare(
"
SELECT *
FROM wppp_postmeta
WHERE meta_key LIKE %s
AND meta_value LIKE %s
",
'contributing_game_creators_%_game_creator_roles',
'%'.$search_value.'%'
)
);
That won't work, I've lost a few hours to it. The ACF repeater fields get a new id that doesn't correspond to the parent-post id (except for the first one). To make this even more confusing: If you delete a repeater field, it can stay in the database; so any queries will return all of the repeater-field data, old & new.
The best workaround I've found for this is to loop the repeater field silently and push the data to a multidimensional array. Then loop over that array again to produce the output.

Wordpress - Retrieving specific posts from the current user

I have a custom post with 2 fields, the first field being the user, and the second one being a table, and whenever a user is logged in, I must display all of the posts that have the user in the 1st field, this is what I found:
In wp_user the ID of the user I'll be using to test it will have: user_id = 3
In wp_postmeta, I have a row with these values:
-post_id=92
-meta_value=3
And in wp_posts, in the ID = 92, I have the specific post name I was looking for.
I'm pretty confused on how to make this code dynamically, so any user will be able to see their own posts. And BTW, the post_author is the admin, so I can't use that field
Basiclly, I must retrieve the post_id from wp_postmeta by only using the the meta_value which I've seen a couple of guides but can't really understand how to do it. I know I must use wp_get_current_user(). Please someone explain me with detail how to achieve this.
Thanks
In your functions.php you need to place this hook , this hook is fired whenever a post is added or updated you need to use this for saving post id in the post meta table.
function enter_post_id( $post_id ) {
wp_get_current_user() // Get current user id with this
update_post_meta()($post_id, $meta_key, $meta_value, $unique)
// add a meta key say 'user_id' and place the value of user_id as meta value
}
add_action( 'save_post', 'enter_post_id' );
Now You have the match completed , you just need to query through the post meta table and get the post id for the user , and then from wp_posts show only the post's with matched ID.
Hope this helps . Feel free to ask if you have any confusion.
No problem :) Use this function.
function get_post_id(){
global $wpdb;
$result = $wpdb->get_results( "SELECT post_id FROM wp_postmeta WHERE meta_key='user_id' AND meta_value= $user_id ", OBJECT );
// Where $user_id is the id you get from wp_current_user()
}
Now var dump this $result to see the post_id
loop through this to get values for each post id

Selecting most recent result from MySQL where there are more than 1 rows matching ID

I'm using wordpress with some custom fields, to cut this short, im echoing out all rows that have the meta_key of post_author that arent empty and grouping likewise names together otherwise i might get Pete, Steve, Pete - when all i need is Pete, Steve. Below is the SQL that makes that work
$wpAuthors = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT(meta_value)
FROM $wpdb->postmeta WHERE meta_value !='' AND meta_key = 'post_author'
ORDER BY meta_key ASC"));
That works great. But what if i change the author FROM pete TO steve? Of course wordpress makes a duplicate entry for that post id, with a different Custom field row id. (Post ID on both rows would be something like 1100 while entry rows might be 3000 and 3001). Now the issue here is that my script still works, because both Pete and Steve are entered into the database in the correct column, but what i need it to do now is to select the most recent copy of that post id row to get the most recent author, does that make sense? I hope so!
define('WP_POST_REVISIONS', false); Add this variable in to wp-config.php, it will prevent wordpress to create post revisions.
After that install this plugin http://wordpress.org/plugins/wp-optimize/screenshots/.
Now remove all post revisions using above wp-optimize plugin.

Categories