I have a "usermeta" table in db.
and there are some columns including meta_key and meta_value.
the delivery_location(meta_key) has same meta_value.
so I want to replace current meta_value to new value in deliery_location
(70982, 20, 'delivery_location', 'a:61:{i:0;s:47:"{lat:32.89030473256227, lng:-96.96264851172401}";i:1;s:47:"{lat:32.89359300394015, lng:-96.94936752319336}";i:60;s:0:"";}'),
my question is that how I can replace the all same meta_values at once in mysql?
You can get all users using get_users(). check the below code. code goes in your active theme functions.php file.
function update_all_user_delivery_location(){
$users = get_users( array( 'fields' => array( 'ID' ) ) );
foreach ( $users as $key => $user ) {
$delivery_location = get_post_meta( $user->ID, 'delivery_location', true );
// modify your code here
update_post_meta( $user->ID, 'delivery_location', $delivery_location );
}
}
add_action( 'init', 'update_all_user_delivery_location', 10, 1 );
Related
In short i want get meta values from usermeta table but the meta value is in serialize array form,
these values are post ids actually, this is my working code for single meta value, i want multiple values from serialize array in meta_value
$user_id = get_current_user_id();
$key = 'classes';
$single = true;
$user_last = get_user_meta( $user_id, $key, $single );
$user_last;
$query_args = array(
'posts_per_page' => $output,
'post_status' => 'publish',
'post_type' => 'stm-courses',
'meta_query' => array(
array(
'key' => 'classes',
'value' => $user_last,
'compare' => 'LIKE'
)
)
);
print_r( $query_args ); echo "string";
the single meta value is working fine but not multiple values
the below is the output of the above query
Array ( [posts_per_page] => 3 [post_status] => publish [post_type] => stm-courses [meta_query] => Array ( [0] => Array ( [key] => classes [value] => Array ( [0] => 5033 [1] => 5034 ) [compare] => LIKE ) ) ) string
and in database the value for meta_key classes is stored something like this
a:2:{i:0;s:4:"5033";i:1;s:4:"5034";}
the values are dynamically changeable so i need something dynamic logic,
Thanks in advance, pls suggest me any good idea how i do this
$meta_query[] = array(
'key' => 'classes',
'value' => sprintf('"%s"', $user_last),
'compare' => 'LIKE',
);
I don't think Jagan's solution will work.
I've the same problem and if i look in the database, a meta_key in a serialize array are in the row meta_value (of an other meta_key )... Logic.
So if you try to get
meta_key => "classes",
i think the result will be null.
If someone have a solution to get the value of a meta_key in a serialize array with a wp_query ? I take it !
I need to count how many times my date recorded in the meta key:metakey_AMC_data, in format (d-m-Y) it is contained in the database by comparing it with the current date
$mostra_data_corrente = date('d-m-Y');
$query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}postmeta
WHERE (meta_key = 'metakey_AMC_data'
AND meta_value = '$mostra_data_corrente')");
$conta_risultati = count($query);
and this I can do perfectly.but now my need is to execute the first query by linking another AND, and specify when the term slug is equal to the category of the event (terms taxonomy), obviously the query is incorrect
SELECT * FROM {$wpdb->prefix}postmeta
WHERE (meta_key = 'metakey_AMC_data'
AND meta_value = '$mostra_data_corrente')
AND(slug = 'aperitivi') "
how can i do this?
You can get that count as well. You need to modify query (code) like follow:
$qry = array(
'post_type' => 'post', // mention your post type to narrow down searching through postmeta table
'meta_query' => array(
array(
'meta_key' => 'metakey_AMC_data',
'meta_value' => $mostra_data_corrente,
'compare' => '='
)
),
'tax_query' => array(
array(
'taxonomy' => 'nameoftaxonomy', // Write the name of taxonomy that you have assinged while you created a CPT (custom post type)
'field' => 'slug',
'terms' => 'aperitivi',
)
)
)
$the_query = WP_Query($qry);
echo $the_query->post_count;
You have to make some necessary changes in above code to suite your requirements. I've added comment where you have to do changes.
The query should return thumbnail, title, name, price etc (all the fields)
<?php
query_posts('meta_key=cp_job&meta_value=Sell');
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
the_content();
//
} // end while
} // end if
?>
It should return results where meta_key=cp_job and meta_value=Sell.. I've tried all sorts of queries and this has taken up several hours, as i've yet to find a solution.
I'm working with a theme and the only time i've gotten a result is with this query
$metakey = 'cp_job';
$job = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
foreach ($job as $value) {
echo $value";
}
This query doesn't do what i want to accomplish.
I simply want "SELECT * FROM table WHERE cp_job='Sell'"; but WordPress makes everything so complicated. I don't even know the table which hold my results!...
Any help please..
You may try this:
$args = array(
'post_type' => 'post', // Or custom post type if it's a CPT
'meta_key' => 'cp_job',
'meta_query' => array(
array(
'key' => 'cp_job',
'value' => 'Sell'
)
)
);
Then run the query and loop the $query same way:
$query = new WP_Query( $args );
On Wordpress, i want to insert data in my database on a table i created for purpose, i'm using the global $wpdb and insert.
I've got many more data to insert, is that possible to insert a foreach or any loop inside my array?
Thank you in advance,
jean-Charles
global $wpdb;
$name="JC";
$email="jcdarocha#gmail.com";
$website="www.jcdarocha.co.uk";
$country="UK";
$message="hello World";
global $wpdb;
$wpdb->insert( 'my_table',
array(
'$name' => '$name',
'$email' => $email,
'$website' => $website,
'$country' => $country,
'$message' => $message
),
array( '%s', '%d' ) );
Use $wpdb->query and concat all data into a string
$insert = "INSERT INTO my_table (name, email, website, country, message) VALUES ";
foreach( $datas as $data ) {
$insert .= sprintf("('%s','%s','%s','%s','%s'),", $data['name'], $data['email'], $data['website'] , $data['country'], $data['message'] )
}
$wpdb->query( rtrim($insert, ','));
global $wpdb;
$name="JC";
$email="jcdarocha#gmail.com";
$website="www.jcdarocha.co.uk";
$country="UK";
$message="hello World";
global $wpdb;
$wpdb->insert( 'my_table',
array(
'name' => $name,
'email' => $email,
'website' => $website,
'country' => $country,
'message' => $message
) );
I am trying to do a custom sql query to get 2 lots of post_meta keys and there values. Here is my code:
<?php
$queried_post = get_post($post_id);
$title = $term->name ;
$userid = $current_user->user_email;
$result = mysql_query("SELECT * FROM wp_postmeta WHERE meta_key='user_programme_name' AND meta_value='".$title."' AND meta_key='user_email' AND meta_value='".$userid."' ");
$num_rows = mysql_num_rows($result);
if ($num_rows > 0 ) {
?>
<p style="width:430px;">You have already requested a place on this course</p>
<?php } ?>
The first part of the query works when i jsut query the first set of keys and values:
meta_key='user_programme_name' AND meta_value='".$title."'
But as soon as i add the second lot:
AND meta_key='user_email' AND meta_value='".$userid."'
It doesn't work. Is there something I'm doing wrong?
Any help would be greatly appreciated.
Cheers, Dan
Use something like this :
$args = array( 'post_type' => 'myposttype',
'posts_per_page' => …,
'offset' => …,
'meta_query'=>array(
'relation' => 'AND',
array(
'key'=>'_first_key',
'value'=> youvalue,
'type' => 'DATE', // etc.
'compare' => '='
),
array(
'key'=>'_second_key',
'value'=> youvalue,
'type' => 'DATE', // etc.
'compare' => '='
)
)
);
$yourloop = new WP_Query( $args );
Fix your SQL syntax for selecting multiple values on the same column:
<?php
$result = mysql_query("SELECT * FROM wp_postmeta WHERE meta_key IN ('user_programme_name', 'user_email') AND meta_value IN ('.$title.', '.$userid.') ");
?>