I have the following SQL query and would like to know if that could be done with the same functionalities as WP query, instead?
$q = "DELETE FROM wp_postmeta WHERE
(
meta_key LIKE '_mepr_stripe_product_id_q3c0oi-7e4%' OR
meta_key LIKE '_mepr_stripe_plan_id_q3c0oi-7e4%' OR
meta_key LIKE '_mepr_stripe_tax_id_q3c0oi-7e4%' OR
meta_key LIKE '_mepr_stripe_initial_payment_product_id_q3c0oi-7e4%' OR
meta_key LIKE '_mepr_stripe_onetime_price_id_%'
)
AND post_id IN ($post->ID);";
global $wpdb;
$wpdb->query($wpdb->prepare($q));
Any ideas?
Thanks
Related
I have a function that works fine when I say
$results = $wpdb->get_results(
"SELECT meta_value
FROM wp_woocommerce_order_itemmeta
WHERE order_item_id = '21'
AND meta_key = '_qty"
);
This function returns 4, which is the correct number I would be looking for in my database.
Unfortunately, this code is returning an empty array to get_results(). There is something wrong in my code with passing a variable to $wpdb, does anyone have an idea?
$results = $wpdb->get_results(
"SELECT meta_value
FROM wp_woocommerce_order_itemmeta
WHERE order_item_id =" .$order_item_id.
"AND meta_key = '_qty'"
);
In this specific case I think it is a simple issue of missing white-space in your string concatenation. However there may be a more suitable way to prepare your statement that would have prevented this by making it easier to spot, and follows other good query building practices
Wordpress Prepared Statments
By using $wpdb->prepare() you can use (most of) the sprintf () syntax to help you build your query.
$query = $wpdb->prepare("SELECT meta_value
FROM wp_woocommerce_order_itemmeta
WHERE order_item_id = %d
AND meta_key = '_qty'", $order_item_id);
$results = $wpdb->get_results($query);
More info available at the wordpress codex and on prepared statements in general
I would like some help iterating through an object that is stored in the meta_value column of my wp_postmeta table.
I have ID's stored in the column and I would like to use those ID's in an inner SELECT that will go and get the Page Title that is associated with each ID.
Here is my SQL:
SELECT p.ID, p.post_title, p.post_status, p.post_type, pm.meta_key, pm.meta_value
FROM wp_posts p, wp_postmeta pm
WHERE p.ID = pm.post_id
AND pm.meta_key = topic_related_topics
AND pm.meta_value != ''
ORDER BY p.post_title, p.post_type desc
Here is an example of what is brought back:
1313,ADIPIC ACID,draft,post,topic_related_topics,
a:5:
{i:0;s:3:"961";
i:1;s:4:"1313";
i:2;s:3:"975";i:
Basically I need to know the title that is associated with ID 961. The title resides in the wp_posts table.
The value you are seeing in the database is an array that has been serialized to a string by PHP. The i stands for "integer" and the s stands for "string", with the number following the s indicating how long the string is. In this case, even though "961" is an integer, it is being stored as a string in the first place of a an array with 5 elements (it looks like you chopped the last 2 off).
There isn't a good way to unserialize the data in SQL, so you will need to do it in PHP and then make a second query to get the title. You can use the WordPress function maybe_unserialize() to convert it back into an array.
global $wpdb;
$sql = "your sql....";
$rows = $wpdb->get_results( $wpdb->prepare( $sql, $your_args ) );
foreach( $rows as $row ){
// convert to an array
$ids = maybe_unserialize( $row->meta_value );
// loop through the array
foreach( $ids as $id ){
// fetch the titles
$sql = "SELECT post_title FROM $wpdb->posts WHERE ID = %d";
$title = $wpdb->get_var( $wpdb->prepare( $sql, $id ) );
}
}
A more standard way of doing it would be to just use get_post_meta() to fetch the array, and have WordPress handle the serialization internally. The other big advantage here is that if you are using caching, support for it is already built into these functions.
Lastly if you do want to be able to fetch it all in a single SQL query, you will need some additional logic, likely hooked to the saving of the meta value. In addition to having it save as an array, you can grab the single value you want to JOIN on and save it in its own meta_key. With a single value you can then join the posts table to the postmeta to accomplish what you are looking for.
I just added a custom field to a specific post_type. Lets say the post type is "Book".
I want to insert into the database default value of 0 for every post_id with post_type Book.
Basically I need to take the post_id, using that post_id add post_id, meta_key, and meta_value to the postmeta table.
some psuedo insert statement might look like this:
INSERT INTO postmeta WHERE (post_type = "Book" FROM other_table GET post_id) post_id = post_id, meta_key = newfield, meta_value = 0
Another solution might be to change how I am querying posts in wordpress:
Currently only posts that have the specific meta_key filled out are being pulled when I query using the meta_key parameter. Is there a way to show posts that also do not have this meta_key? I need it for sorting the posts that do have the meta_key.
Edit:
I tried this in the wp database:
INSERT INTO wp_postmeta(`post_id`,`meta_key`,`meta_value`) VALUES((SELECT ID FROM wp_posts tb WHERE tb.post_type = 'support'),'category_order','0')
This is basically what I want to do except it should allow for multiple values. It currently does not.
I had to do a job like this in the past and I stick to the Wordpress Foundations. I would run away from SQL while adding data to posts.
Basically I did a page template and inside I would just loop the posts and use update_post_meta, like this:
<?php
$args = array(
'post_type'=> 'book',
);
query_posts( $args );
// the Loop
while (have_posts()) : the_post();
//update the post meta, while looping
update_post_meta($post->ID, 'fieldname', 0);
endwhile;
?>
Just tested the code and it's working.
I need some help updating the price of a wp table. I am trying to do this outside of wordpress running a php script on the server.
This the table "wp_pzvy_postmeta" which has these four columns:
meta_id post_id meta_key meta_value
18538 4356 _sku GF-2070
18541 4356 _price 2.343
I need to be able to update the meta_value 2.343 based on the meta_value GF-2070, as you can see they both have the same Post_id (4356).
I do ok with php but this mysql stuff really drives me crazy.
I know how to connect to the database but after that I don't know what to do.
I guess I need to be able to retrieve the post_id based on the meta_value (GF-2070) and then use it on the UPDATE
$sql= "update `wp_pzvy_postmeta` set meta_value=5 where meta_key ='_price' and post_id = $postid";
I suggest to work with mysqli:
Create an mysqli host:
$mysqli = new mysqli("host", "user", "password", "database");
Update your $sql query:
a) Get the post_id you want:
select post_id from wp_pzvy_postmeta where meta_key = '_sku' and meta_value = #your_value#
b) Pick your expression :
update wp_pzvy_postmeta set meta_value = 5 where meta_key = '_price' and post_id = #exp_a#
c) Join everything:
Replace #your_value# with 'GF-2070'
Replace #exp_a# with a)
$sql = b)
Make your query, using the previous object:
$result = $mysqli->query($sql);
And you're ready to go! Check here for an working example :)
I am trying to show the sum of meta_value (which is numeric) for all the instances where post_id is the same in table postmeta. From looking at other replies the following seems to be what should do this but I just get nothing returned
<?php
global $wpdb;
$data = $wpdb->query("
SELECT SUM(meta_value)
FROM $wpdb->postmeta
WHERE `post_id` = $post_id
");
echo $data;
?>
I can't figure out what is wrong with this when compared to other answers. Any pointers?
Thanks
Well... Are you connected to the database? And when you're including object properties inside a string, it's good form to encase them in braces, like so:
$data = $wpdb->query("SELECT SUM(meta_value)
FROM {$wpdb->postmeta}
WHERE `post_id` = $post_id
");
Encapsulate your php variables if they are complex, so:
FROM {$wpdb->postmeta}
For SELECTing, you should use $wpdb->get_results() instead of $wpdb->query().
$wpdb->query() "… returns an integer corresponding to the number of rows affected/selected."
http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results
OK, I have changed the code to reflect the above including he encapsulation and using get_results instead of query but now just get 'Array' as the returned result.
global $wpdb;
$data = $wpdb->get_results("
SELECT SUM($wpdb->postmeta.meta_value)
FROM {$wpdb->postmeta}
WHERE `post_id` = the_id()
");
echo $data;
Do I need to use a foreach() loop to cycle the resultant array? I have also checked there is data and also that the_id() is valid