I've got an invalid argument supplied for my last associative array foreach loop. I am looping through different email addresses and creating an array. Am I doing something wrong? Should I create a null array?
Cheers!
global $wpdb, $wpsc_variations;
$stock_table = $wpdb->prefix . 'postmeta';
$posts_table = $wpdb->prefix . 'posts';
$q = 'SELECT DISTINCT post_id FROM ' . $stock_table . ' WHERE meta_key=\'' . NOTIFY_META . '\' AND meta_value=1';
$q_assoc_arr = $wpdb->get_results($q, ARRAY_A);
foreach($q_assoc_arr as $row) {
$product_id = $row['post_id'];
$product_data = get_post_custom( $product_id );
$product_data['meta'] = maybe_unserialize( $product_data );
foreach ( $product_data['meta'] as $meta_key => $meta_value ) {
$product_data['meta'][$meta_key] = $meta_value[0];
}
if ($product_data['meta']['_wpsc_stock'] > 0) {
foreach (get_post_meta($product_id, NOTIFY_EMAILS_META, true) as $k=>$v) {
$emails[] = $v;
}
Your third foreach relies on the output of get_post_meta, but you're passing true as the 3rd parameter. This tells get_post_meta to return a single value as a string, not an array.
foreach expects an array to iterate over. :)
Note: This part will fail for certain, but your other instances of foreach may also fail if an array is not passed.
Related
I have an ACF repeater field field_5f71d4eaaf381 for email addresses and I want to output a comma separate list of the sub field values (measurement_extra_email_address) to create a $to for the WordPress mail function.
$values = $_POST['acf'];
$extra_recipients = $_POST['acf']['field_5f71d4eaaf381'];
if ( $extra_recipients ) {
$list = array();
foreach( $extra_recipients as $extra_recipient ) {
$list[] = $extra_recipient['measurement_extra_email_address'];
}
$to = implode(',', $list);
}
I thought above code should/would do it but no email is being send so I guess the code is incorrect..?
Just a note: When I remove above part of the code and just try $to = 'my#emailadress.com'; an e-mail is being send so the error should be in there...
Here's the code I was looking for:
$values = $_POST['acf'];
$measurement_mail_extra_recipients = $_POST['acf']['field_5f71d4eaaf381'];
if ( $measurement_mail_extra_recipients ) {
$list = array();
foreach( $measurement_mail_extra_recipients as $measurement_mail_extra_recipient ) {
$list[] = $measurement_mail_extra_recipient['field_5f71d55aaf382', $measurement_client];
}
$to = implode(',', $list);
}
I'm looking for a solution in $wpdb->get_results() which is equivalent PDO::FETCH_NUM.
This is what I'm trying to achieve:
function get_data( $token, $field ){
global $wpdb;
$wpdb->show_errors();
$result = $wpdb->get_results('SELECT '.$field.' FROM ' . $this->get_table(). ' WHERE token =\''.$token.'\' ORDER BY id DESC');
return $result[0];
}
As you can see from my query above, I'm selecting specific columns from the database, but I don't know how to access selected column using $wpdb->get_results();
I would like to get the value of the $field column based on the where clause criteria.
After deep research I have gotten a solution.
Basically, this is how to get the $field column.
function get_data( $token, $field ){
global $wpdb;
$wpdb->show_errors();
$result = $wpdb->get_results('SELECT '.$field.' FROM ' . $this->get_table(). ' WHERE token =\''.$token.'\' ORDER BY id DESC');
$data = "";
foreach ($result as $key => $value) {
$data = $value->$field;
}
return $data;
}
To make it simple, let's assume you have a column called first_name, this is how to get the first_name column from the database.
function get_data( $token ){
global $wpdb;
$wpdb->show_errors();
$result = $wpdb->get_results('SELECT first_name FROM your_table_name WHERE token =\''.$token.'\' ORDER BY id DESC');
foreach ($result as $key => $value) {
echo $data = $value->first_name;
}
}
I want to add a shortcode which will execute a DB query and return the result.
Here's my functions.php:
function get_posts_count($cat){
global $wpdb;
$a = shortcode_atts( array(
'id' => ''
), $cat );
$id=$a['id'];
$count=$wpdb->get_results( "SELECT `count` FROM `wpmy_term_taxonomy` WHERE `term_id`=$id");
foreach($count as $row)
echo '('.$row->count.')';
}
add_shortcode( 'postCount', 'get_posts_count' );
This is the shortcode in the editor:
And here's the end result:
The value in this case 1 appears above the text Real Estate. How can I make sure it is displayed within the line?
Thanks in advance
the shortcode accepts parameters (attributes) and return a result (the shortcode output). If the shortcode produces a HTML then ob_start can be used to capture output and convert it to a string as follows:-
function get_posts_count( $cat ) {
ob_start();
global $wpdb;
$a = shortcode_atts( array(
'id' => '',
), $cat );
$id = $a['id'];
$count = $wpdb->get_results( "SELECT `count` FROM `wpmy_term_taxonomy` WHERE `term_id`=$id" );
foreach ( $count as $row ) {
echo '(' . $row->count . ')';
}
return ob_get_clean();
}
add_shortcode( 'postCount', 'get_posts_count' );
PHP newb here, looking for some guidance. I am working with BuddyPress and Advanced Custom Fields (ACF). I have an ACF field 'new_user' with a value of true/false. I am trying to filter my BuddyPress Members Loop to only display users with a value of 'new_user' = true.
There are 2 code samples here.
The standard BP Members Loop. My thought here, is how do I first query my users by ACF ‘new_user’ = true and then start the bp member loop?:
if ( bp_has_members() ) :
// some code goes here
endif;
while ( bp_members() ) : bp_the_member();
//OUTPUT MEMBERS LIST HERE
endwhile;
This is a BP function to filter by Buddypress extended user fields. The idea here I believe is to replace the code in the middle specific to xprofile_get_field with the proper ACF code:
function my_custom_ids( $field_name, $field_value = '' ) {
if ( empty( $field_name ) )
return '';
global $wpdb;
$field_id = xprofile_get_field_id_from_name( $field_name );
if ( !empty( $field_id ) )
$query = "SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id;
else
return '';
if ( $field_value != '' )
$query .= " AND value LIKE '%" . $field_value . "%'";
/*
LIKE is slow. If you're sure the value has not been serialized, you can do this:
$query .= " AND value = '" . $field_value . "'";
*/
$custom_ids = $wpdb->get_col( $query );
if ( !empty( $custom_ids ) ) {
// convert the array to a csv string
$custom_ids_str = 'include=' . implode(",", $custom_ids);
return $custom_ids_str;
}
else
return '';
}
Of course, I am open to solving this in another way as well. I hope this is clear.
This is a self Q&A
I need a way to neatly build a key=>value array of all the variations for an item in a cart. The available Woo functions all return a string.
This function takes a cart item, and returns a key=>value array.
Place it in functions.php
/*
* Get a cart product/item's variation as a key=>value pair
*/
function store_get_cart_product_variations($cart_item) {
$variations = WC()->cart->get_item_data($cart_item, true);
// Explode and trim
$parts = explode(PHP_EOL, $variations);
$parts = array_filter($parts);
// Build a key=>value pair, trim any extra whitespace
$variations = array();
foreach($parts as $part) {
list($key, $value) = explode(':', $part);
$variations[trim($key)] = trim($value);
}
return $variations;
}
And then it can be used like this:
$cart_items = WC()->cart->get_cart();
foreach ( $cart_items as $cart_item_key => $cart_item ) {
$variations = store_get_cart_product_variations($cart_item);
foreach($variations as $type => $value) {
echo $type . ': ' . '<span class="value">' . $value .'</span>';
}
}