update_post_meta( $order_id, ‘tracking_number’, $tracking_number );
What did I do wrong? How can I update the tracking number with the order id, on php? can you show me? with this plugin?
Now I need to import multiple records from an external source, and I have an external script which POSTs to accomplish this.
I have tested many times POSTing a single record from my external script and it appears to be working as expected. i.e. record is created if it does not exist, meta values are added and values are set correctly. Repeating this one single POST shows update_post_meta() returning false in my debugging as expected (since values already exist), and the meta values continue to appear correctly in the MySQL database and Admin UI. However..
Here is my problem: When I invoke my external script with two or more CPT updates in two POSTs, previously correct values for all all except the last update appear empty in the Admin UI, and NULL in the database.
My external script sends stringified json in the POST so I have this:
if ($_SERVER['REQUEST_METHOD'] == "POST"){
$json = file_get_contents('php://input');
if ($json){
$data = json_decode($json, true);
$registry = new My_Registry($data);
$registry->create_or_update();
}
}
-and here is the create_or_update() method that is called:
function create_or_update(){
$post_id = $this->_exists();
if ( ! $post_id ){
$post_id = wp_insert_post(array (
'post_type' => 'gwc_registry',
'post_title' => $this->title,
'post_status' => 'publish',
'comment_status' => 'closed',
'ping_status' => 'closed'
));
}
if ( $post_id ){
$this->registry_cpt_id = $post_id;
}
$this->_update_meta();
}
_exists() returns false or the post_id if found (I’m trying to keep code lines down, but can share if helpful).
Here is the essence of _update_meta()
private function _update_meta(){
$cpt_meta_fields = (array)$this;
foreach ($cpt_meta_fields as $key => $value){
$clean_key = (explode("\0", $key))[2];
$registry_meta_key = 'registry_' . $clean_key;
var_dump($registry_meta_key . " > " . $value); //**see comment below
//....//
update_post_meta( $this->registry_cpt_id, $registry_meta_key, $value );
}
}
The debug line at ** always shows correct meta data right before the call to update_post_meta() in my testing, but when I post two records from my external script the first one still gets overwritten with NULLS as described at the top.
This is not a timing issue – I have tried putting a delay in my external script, and I can watch the correct meta data get written into the database for the first record and subsequently get set to NULL when the second record gets processed – even though update_post_meta() returns false for that ‘nullified’ record at the same time!
This appears to be something in the way I am using update_post_meta here but I am at a loss as to how to proceed, and would really appreciate any hints as to where I might dig next.
Thank you in advance for any advice..
]]>I used WCK Pro to create a post type ‘distributors’. Then to create a custom metabox that’s a repeater with metadata id ‘orderdetails’. It has 3 fields and as many rows as you want.
If I enter data into the repeater via the edit post screen, it enters just fine and you can see the data in the template on the front end.
There is also a way for distributors (as a user type) can log in and from the front end submit an order using a custom form. That custom form, upon submit sends a couple of emails, builds the form fields into an array and then uses update_post_meta to upload that array to the meta box (e.g. update_post_meta(get_the_ID(),’orderdetails’,$orderArray); )
This works just fine as long as NO data was ever entered through the back end system (e.g. the WCK interface on the post edit screen). However, if data was ever entered via the WCK interface on the post edit screen, programmatic updates to post meta stop working — and delete_post_meta also does not work for some reason, so it’s not like I can simply delete whatever’s there and replace it with the array that’s built in the template once the form is submitted.
I get this is a nuanced issue, but hoping maybe someone else has run into this problem and found a solution.
]]>When we use “update_post_meta” we are able to save data to
_yoast_wpseo_title
_yoast_wpseo_focuskw
_yoast_wpseo_metadesc
like this: update_metadata($id, ‘_yoast_wpseo_title’, ‘value-to-save’);
We are then able to retrieve that data without issue.
The problem we have is that the data we save does not appear on Yoast’s edit fields on the item in question. When we look at the page’s source code it is not there either.
Data is not recorded to ‘yoast_indexable’ when using update_metadata — Only to ‘termmeta’.
Data entered using the “editor” and pressing update/publish does get recorded properly and appears on both ‘yoast_indexable’ and thus on the page’s source code.
We are using the most up to date version of WP (Version 5.5.3) and YoastSEO (15.3).
About 7 months ago Collins Agbonghama asked (https://yoast.com/yoast-seo-14-0/) if this would continue to work to which the answer was “yes”. We have been unable to find if it still should work or if there is now another process.
Thank you again and for your insights into we might be doing wrong.
]]>Update – I have tried below hooks // the action is getting triggered but the fields are not updating
1. publish //works but no update is happening
2. save_post && save_post_l //works but no update is happening.
add_action('transition_post_status', 'updated_to_publish', 10, 3);
function updated_to_publish($new_status, $old_status, $post)
{
if (($new_status == 'publish') && ($post->post_type == 'l')) {
$post_id = $post->ID;
if (get_field('advanced_option_edit_seo', $post_id)) {
if (defined('WPSEO_VERSION')) {
$metatitle = get_field('seo_title', $post_id);
$metadesc = get_field('seo_meta_description', $post_id);
$metakeywords = get_field('seo_keyword', $post_id);
update_post_meta($post_id, '_yoast_wpseo_title', $metatitle );
update_post_meta($post_id, '_yoast_wpseo_metadesc', $metadesc );
update_post_meta($post_id, '_yoast_wpseo_focuskw', $metakeywords);
}
}
} else {
return;
}
}
]]>I downloaded my DB so I could look at how the data is being stored for a file_list.
Some of it I can understand:
(2466, 768, '_cmb_file_list', '
a:2:{
i:770; //image ID
s:86:"https://www.mydomain.com/wp-content/uploads/2018/05/xxx.jpg";
i:721; //image ID
s:89:"https://www.mydomain.com/wp-content/uploads/2013/07/yyy.jpg";
}
'),
I understand how to use update_post_meta, and I understand how to get the ID and the URL for the photos saved in the older “file” CMB field types.
In the above data, I’m not sure what the “a:2” and the “s:86” and the “s:89” signify.
Can you advise me how I could use update_post_meta or another function to move image data from a series of “file” objects to a single “file_list”?
I appreciate it!
]]>Here’s how I’m trying to save the options, with some notes after. Please let me know if you have any ideas or need more info:
add_action( 'save_post', 'save_theme_options' );
function save_theme_options( $post_id ) {
global $theme_options;
foreach ( $theme_options->options as $option ) {
if ( array_key_exists( $option->id, $_POST ) ) {
update_post_meta($post_id, $option->meta_key, $_POST[$option->id]);
}
}
}
$option->id is actually a text value in my object, such as ‘example_option’.
]]>Following the advice on https://codex.www.ads-software.com/Function_Reference/update_post_meta#Character_Escaping I tried the following:
<?php
$escaped_meta = "field1\\r\\nfield2\\r\\nfield3";
update_post_meta( $id, 'double_escaped_json', wp_slash( $escaped_meta ) );
?>
This stored the \\r\\n in the database. Removing the double slash in the $escaped_meta variable leads to \r\n being stored literally in the database (so it is being sent as \\r\\n). Removing the wp_slash() leads to rn being stored in the database with no escaping.
Is there a reliable doing this such that the actual data sent to the database is: UPDATE postmeta SET value='field1\r\nfield2\r\nfield3' WHERE id='$id'
?
I can’t even get it to return a true/false, while other functions work normally
I’m doing this in a solo php file with the WordPress environment loaded. I never had any problems with this before.
]]>Thanks!
]]>