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
  • How is the meta data added otherwise? Is this an ACF field? Something saved by another plugin? It's near impossible to answer this if nothing is known about the field itself and how it's meant to be stored – Tom J Nowell Commented Feb 18, 2020 at 15:47
  • I try to suggest: $unique_ids should be initialized as empty array before the loop, otherwise the conditional if ( 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
  • Sorry, I was trying to boil this down and I guess I didn't leave enough info to work with. – Curtis B Commented Feb 18, 2020 at 16:45
  • To answer some of your questions: • This meta data is added by another plugin (Modern Tribe's Event's Calendar) after a sale is made for a ticket through WooCommerce. This is essentially how that plugin adds the data - most of the lines here are pulled from their function during ticket creation. Part of the challenge is that I'm working with an array now instead of a single string. • Unique_ids is initialized before this block. I'll update the above with the actual DB query and other statements. – Curtis B Commented Feb 18, 2020 at 16:51
Add a comment  | 

1 Answer 1

Reset to default 0

My 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