admin管理员组文章数量:1398773
I'm sure I'm missing something obvious, but how can I have this function add a meta value if the meta value doesn't already exist in a foreach loop?
You can see some of my ideas in the comments here as well.
php
global $wpdb;
$order_id = $order_data["id"];
$names = array();
$unique_ids = array();
$new_ticket_links = array();
$base_ticket_path = esc_url( home_url( '/' ) . 'tickets_download/');
$ticketidmeta = '_tribe_ext_pdf_tickets_unique_id';
$table_name = $wpdb->prefix . "postmeta";
$db_data = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $table_name WHERE `meta_value` LIKE $order_id AND post_id <> $order_id");
if (!empty($db_data)) {
foreach ($db_data as $value) {
$names[] = $value->post_id;
}
}
if (!empty($names)) {
foreach ($names as $val) {
$unique_ids[] = get_post_meta($val, $ticketidmeta, false);
// If the unique_id isn't there, create it
if ( empty( $unique_ids ) ) {
/*
* I've played with a foreach loop here as well
* The results are spotty - doesn't seem to work.
*/
// foreach ($names as $val) {
$unique_id = uniqid( '', true );
// uniqid() with more_entropy results in something like '59dfc07503b009.71316471'
$unique_id = str_replace( '.', '', $unique_id );
$unique_id = sanitize_file_name( $unique_id );
add_post_meta( $val, $ticketidmeta, $unique_id, true );
// }
}
}
foreach ($unique_ids as $unique_id) {
$new_ticket_links[] = $base_ticket_path . implode($unique_id);
}
}
I'm sure I'm missing something obvious, but how can I have this function add a meta value if the meta value doesn't already exist in a foreach loop?
You can see some of my ideas in the comments here as well.
php
global $wpdb;
$order_id = $order_data["id"];
$names = array();
$unique_ids = array();
$new_ticket_links = array();
$base_ticket_path = esc_url( home_url( '/' ) . 'tickets_download/');
$ticketidmeta = '_tribe_ext_pdf_tickets_unique_id';
$table_name = $wpdb->prefix . "postmeta";
$db_data = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $table_name WHERE `meta_value` LIKE $order_id AND post_id <> $order_id");
if (!empty($db_data)) {
foreach ($db_data as $value) {
$names[] = $value->post_id;
}
}
if (!empty($names)) {
foreach ($names as $val) {
$unique_ids[] = get_post_meta($val, $ticketidmeta, false);
// If the unique_id isn't there, create it
if ( empty( $unique_ids ) ) {
/*
* I've played with a foreach loop here as well
* The results are spotty - doesn't seem to work.
*/
// foreach ($names as $val) {
$unique_id = uniqid( '', true );
// uniqid() with more_entropy results in something like '59dfc07503b009.71316471'
$unique_id = str_replace( '.', '', $unique_id );
$unique_id = sanitize_file_name( $unique_id );
add_post_meta( $val, $ticketidmeta, $unique_id, true );
// }
}
}
foreach ($unique_ids as $unique_id) {
$new_ticket_links[] = $base_ticket_path . implode($unique_id);
}
}
Share
Improve this question
edited Feb 18, 2020 at 16:52
Curtis B
asked Feb 18, 2020 at 15:37
Curtis BCurtis B
133 bronze badges
4
|
1 Answer
Reset to default 0My problem was just trying to do too much in at once. In the main foreach loop:
if (!empty($names)) {
foreach ($names as $val) {
// Get the pdf's unique_id if it's in the db already
$unique_id = get_post_meta($val, $ticketidmeta, false);
// If the unique_id isn't there, create it
if ( empty( $unique_id ) ) {
$unique_id = uniqid( '', true );
// uniqid() with more_entropy results in something like '59dfc07503b009.71316471'
$unique_id = str_replace( '.', '', $unique_id );
$unique_id = sanitize_file_name( $unique_id );
add_post_meta( $val, $ticketidmeta, $unique_id, true );
}
// Use that unique id to make the ticket path
$ticketurl = sprintf( $base_ticket_path . sprintf(implode($unique_id)));
// Add each unique id to an array
$uniqueIDs[] = $ticketurl;
}
}
$attendees_data = sprintf(implode($uniqueIDs));
In my original question, I was trying to set the final array at the first statement of the foreach, and check if the metadata was there, and assign that metadata to the value in the final array.
Instead, now for each $val we first try to get the post meta data, then if it doesn't exist we create it, then we do some structuring of that metadata, then we assign it to the final array variable.
As you can tell from the last line there's more work to do on this project, but thank you for everyone who tried to help!
本文标签: phpIn a foreach loophow can I add a meta value if it doesn39t exist
版权声明:本文标题:php - In a foreach loop, how can I add a meta value if it doesn't exist? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744732441a2622128.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$unique_ids
should be initialized as empty array before the loop, otherwise the conditionalif ( empty( $unique_ids ) )
will fail as soon as the array got populated by the first found post_meta ( just an idea, without reading the full code) – Andrea Somovigo Commented Feb 18, 2020 at 15:53