I tried to make a complex query to WordPress database but it did not work, so I ask for help.
First I need to get the meta value from one table, and then using it to get the ID from another table.
For this I use the following code:
global $wpdb;
$event_ID = 306;
$meta_value = $wpdb->get_var( "SELECT meta_value FROM wp_postmeta
WHERE meta_key = 'driver_type' AND post_id = '{$event_ID}'" );
$event_type = $wpdb->get_var( "SELECT post_title FROM wp_posts
WHERE ID = '{$meta_value}'" );
I would like to optimize the query. Tried to make here such request but it returns to me 1 instead of the necessary value:
$complex = $wpdb->query( "SELECT t2.post_title, t1.meta_value FROM wp_postmeta AS t1
INNER JOIN wp_posts AS t2 ON t1.meta_value = t2.ID
WHERE meta_key = 'driver_type' AND post_id = '{$event_ID}' GROUP BY t1.post_id" );
Please tell me how to make the correct complex query in my case.
You should be able to just chain together your two queries:
SELECT post_title
FROM wp_posts
WHERE ID = (SELECT meta_value FROM wp_postmeta
WHERE meta_key = 'driver_type' AND post_id = '{$event_ID}');
Or, you could try doing a join:
SELECT p.post_title
FROM wp_posts p
INNER JOIN wp_postmeta m
ON p.ID = m.meta_value
WHERE
m.meta_key = 'driver_type' AND post_id = '{$event_ID}';
Related
I have the following wordpress query which is displaying the post title multiple times, I have checked and it's getting all the revisions for each post.
Here is the query:
SELECT DISTINCT post_title, ID
FROM wpblog_posts
WHERE post_title LIKE '%Kimberley%'
OR post_title LIKE '%Camping%'
AND wpblog_posts.post_type = 'post'
AND post_status = 'publish'
ORDER BY post_title DESC LIMIT 0, 6;
Anyone know why this might be happening.
Update
Removed from query string as not relevant and will make it
easier to debug.
LEFT JOIN wpblog_term_relationships rel ON rel.object_id = wpblog_posts.ID
LEFT JOIN wpblog_term_taxonomy tax ON tax.term_taxonomy_id = rel.term_taxonomy_id
LEFT JOIN wpblog_terms t ON t.term_id = tax.term_id
Cheers
Check this query if it works for you:
SELECT DISTINCT post_title, ID
FROM wpblog_posts
WHERE (post_title LIKE '%Kimberley%' OR post_title LIKE '%Camping%')
AND wpblog_posts.post_type = 'post'
AND post_status = 'publish'
ORDER BY post_title DESC LIMIT 0, 6;
The query was looking for posts LIKE '%Kimberley%' - any type of post OR LIKE '%Camping%' AND wpblog_posts.post_type = 'post' ....
I have read that if there is data missing from the '$wpdb->get_results' query, then the result will be completely empty.
My query is as follows:
$results = $wpdb->get_results("
SELECT
( SELECT guid FROM ch_posts WHERE id = m.meta_value ) AS thumbnail, ID, post_title, post_name, SUBSTR(post_content, 20) AS content, post_date, t.name AS category
FROM ch_posts, ch_postmeta m
JOIN ch_term_relationships tr
JOIN ch_terms t ON t.term_id = tr.term_taxonomy_id
JOIN ch_term_taxonomy tx ON tr.term_taxonomy_id = tx.term_taxonomy_id
WHERE post_status = 'publish'
AND post_author = $user_ID
AND post_type = 'product'
AND ch_posts.ID = m.post_id
AND m.meta_key = '_thumbnail_id'
AND tr.object_id = ch_posts.ID
AND tx.taxonomy = 'product_cat'
");
then
foreach ($results as $result) { }
Because my post has a missing 'thumbnail' record, the entire results set does not get displayed. Is it possible to modify my query to show the results that are found and just ignore the ones that are missing? Or replace the missing values with something else?
I have tried using CASE, But to no avail.
Any help will be most welcome
1st, your query is not full. Show the whole query. 2nd, you're getting thumbnail url in a wrong way. 3rd, join should not be inner for tables that are for getting thumbnail. Example of getting 20 last posts with thumbnail (NULL is none)
SELECT `posts`.`ID` AS `ID`, `posts`.`post_title` AS `title`, `posts`.`post_name` AS `slug`, `posts`.`post_content` AS `content`, `posts`.`post_excerpt` AS `excerpt`, `thumb_link` FROM `wp_posts` AS `posts`
LEFT JOIN (select `thumb`.`post_id` AS `object_id`, `thumbs_link`.`meta_value` AS `thumb_link` from `wp_postmeta` AS `thumb`
LEFT JOIN`wp_postmeta` AS thumbs_link ON (`thumbs_link`.`post_id` = `thumb`.`meta_value`)
AND `thumbs_link`.meta_key = "_wp_attached_file" WHERE `thumb`.`meta_key` = '_thumbnail_id'
GROUP BY `thumb`.`post_id` ) AS `thumb` ON `thumb`.`object_id` = `posts`.`ID`
WHERE `posts`.`post_status` = 'publish'
ORDER BY `posts`.`ID` DESC
LIMIT 20
I am trying to get data with SQL from my Wordpress database by a JOIN, but I do not get it working.
What I need:
Posts where the meta_key "gtp_product_dont_show" exists and where the meta_value not is "true".
And posts where the meta_key "gtp_product_dont_show" does not exists.
My Problem:
I now only get the results where posts have a meta_key "gtp_product_dont_show", but there are also posts which don't have this key, and I need these as well.
This is what I have now:
SELECT
ID, post_title
FROM
wp_posts p
JOIN wp_postmeta m ON
p.ID = m.post_id AND
m.meta_key = 'gtp_product_dont_show' AND
m.meta_value != 'true'
WHERE
post_type = 'products' AND
post_status = 'publish'
Output:
You need a left join:
SELECT ID, post_title
FROM wp_posts p LEFT JOIN
wp_postmeta m
ON p.ID = m.post_id AND
m.meta_key = 'gtp_product_dont_show'
WHERE (m.meta_value is null or m.meta_value <> 'true') and
post_type = 'products' AND
post_status = 'publish';
The left join looks for an appropriate key in the wp_postmeta table. The where clause then says "keep a record when there is no match or the value is not true" -- which I think is the logic you are looking for.
You're looking for this?
SELECT
ID, post_title
FROM
wp_posts p
WHERE
post_type = 'products' AND
post_status = 'publish' AND
not exists (
select 1
from wp_postmeta m
where
p.ID = m.post_id AND
m.meta_key = 'gtp_product_dont_show' AND
m.meta_value = 'true')
This will fetch all the rows from wp_posts, but leave out those where row is found from wp_postmeta where meta_key is gtp_product_dont_show and value is true.
You can use the OR operator to consider both conditions. Try this:
SELECT stuff
FROM myTable
JOIN otherTable ON
(m.meta_key = 'gtp_product_dont_show' AND m.meta_value != 'true') OR
(m.meta_key != 'gtp_product_dont_show')
...
Just a side note, I don't recommend storing booleans as strings like that. You should consider using a TINYINT() where boolean fields are stored as 0 for false, or 1 for true.
I want a mysql query to get all my orderd on pending status. Somebody know how to do that.
I'm using woocommerce ina wordpress website, but I just want the mysql query not with wordpress functions.
Thanks
You really should use the built-in database functions since that complies with the Wordpress coding standards.
You could look at the WC_Query class.
Or you could try something like the below. You will need to change the meta key to whatever the meta key is they are using. Also the meta value might be different than pending and the post_type may be different than 'shop_order'.
$pending = new WP_Query(
array(
'post_type' => array('shop_order'),
'meta_query' => array(
array(
'key' => 'status',
'value' => array('pending'),
)
)
)
);
Here is one example on how to do a meta query of woocommerce orders.
OK guys, it is a weird issue, but I fixed it by simply using a custom query. Somehow adding 'post_status' => 'wc-pending' doesn't actually change the query, but if I use pending, the query changes.
So what I did was using that custom query and modify pending to wc-pending.
Well I spent the weekend working on the query and here is the result:
select
nombre,apellido,direccion,direccion2,codigo,poblacion,provincia,correo, telefono
from (
select
(select meta_value from wp_postmeta pm1 where p.ID = pm1.post_id and meta_key = "_billing_first_name") as nombre,
(select meta_value from wp_postmeta pm1 where p.ID = pm1.post_id and meta_key = "_billing_last_name") as apellido,
(select meta_value from wp_postmeta pm1 where p.ID = pm1.post_id and meta_key = "_billing_address_1") as direccion,
(select meta_value from wp_postmeta pm1 where p.ID = pm1.post_id and meta_key = "_billing_address_2") as direccion2,
(select meta_value from wp_postmeta pm1 where p.ID = pm1.post_id and meta_key = "_customer_user") as codigo,
(select meta_value from wp_postmeta pm1 where p.ID = pm1.post_id and meta_key = "_billing_city") as poblacion,
(select meta_value from wp_postmeta pm1 where p.ID = pm1.post_id and meta_key = "_billing_state") as provincia,
(select meta_value from wp_postmeta pm1 where p.ID = pm1.post_id and meta_key = "_billing_email") as correo,
(select meta_value from wp_postmeta pm1 where p.ID = pm1.post_id and meta_key = "_billing_phone") as telefono
from
wp_posts AS p
WHERE post_type = "shop_order" and id in (SELECT object_id
FROM wp_posts
LEFT OUTER JOIN wp_term_relationships ON wp_posts.ID=wp_term_relationships.object_id
WHERE post_type = "shop_order"
AND term_taxonomy_id=9
)
) A
I'm using Wordpress and I have to figure out how to get multiple values out of the same column and turn them into variables.
The last step is I just want the data to display in a table like so:
Bob Company1 <br>
Alex Company2
Instead I get either Bob Alex Company1 Company 2 or
Bob <br>
Alex<br>
Company1<br>
Company2
Here's two different versions I'm working with:
$sql = "SELECT meta_value as guest from wp_postmeta INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id WHERE post_type='guests' AND meta_key='guest_name' UNION SELECT meta_value as company from wp_postmeta INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id WHERE post_type='guests' AND meta_key='guests_company'";
$result = mysql_query($sql);
$NumberOfResults=mysql_num_rows($result);
if (mysql_num_rows($result) == 0) {
echo "No Guests yet... stay tuned!";
exit;
}
while(list($guest,$company)= mysql_fetch_row($result))
{
echo "<table><tr><td>".$guest."</td><td>".$company."</td></tr></table>";
}
or
$sql = mysql_query("SELECT meta_value as guest from wp_postmeta INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id WHERE post_type='guests' AND meta_key='guest_name' LIMIT 2 UNION SELECT meta_value as company from wp_postmeta INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id WHERE post_type='guests' AND meta_key='guests_company'");
$i = 1;
while ($get = mysql_fetch_array($sql))
{
echo '<table><tr><td>'.$get["guest"].'</td><td>'.$get["company"].'</td></tr></table>';
$i++;
}
ANY HELP would be so appreciated!!! thanks!
Edit: In case anybody in the future wants to create their own widget using advanced custom fields in Wordpress, here's the final product more or less:
$sql = "SELECT p.ID AS post_id, g.meta_value as guest, c.meta_value as company, d.meta_value as date
FROM wp_posts p
JOIN wp_postmeta g ON g.post_id = p.id AND g.meta_key = 'guest_name'
JOIN wp_postmeta c ON c.post_id = p.id AND c.meta_key = 'guests_company'
JOIN wp_postmeta d ON d.post_id = p.id AND d.meta_key = 'show_date'
WHERE p.post_status = 'publish'
ORDER by d.meta_value DESC";
$query = mysql_query($sql);
echo '<table><tr><th>Guest</th><th>Company</th><th>Show Date</th></tr>';
while ($get = mysql_fetch_array($query)) {$newDate = date("m-d-Y", strtotime($get["date"]));
echo '<tr><td>'.$get["guest"].'</td><td>'.$get["company"].'</td><td>'.$newDate.'</td></tr>';
}
echo '</table>';
Really appreciated doublesharp's help on this.
If I understand your question correctly, you have to write the code to display table like this:
echo '<table>';
while ($get = mysql_fetch_array($sql))
{
echo '<tr><td>'.$get["guest"].'</td><td>'.$get["company"].'</td></tr>';
}
echo '</table>';
If the postmeta values are attached to different posts as indicated by the different 'post_type' values in your query, then you will need some other identifying information to link them together. If there is a single post_id that has both a guest_name and guest_company meta_value then you can join it twice to get the results. Using a UNION will always result in them coming back in different rows.
// Join the postmeta table to posts twice, once for each meta_key.
$sql = <<<SQL
SELECT p.ID AS post_id, g.meta_value as guest, c.meta_value as company
FROM wp_posts p
JOIN wp_postmeta g ON g.post_id = p.id AND g.meta_key = 'guest_name'
JOIN wp_postmeta c ON c.post_id = p.id AND c.meta_key = 'guests_company'
WHERE p.post_status = 'publish'
SQL;
// Execute the query
$query = mysql_query($sql);
// Start your table for output
echo '<table>';
while ($get = mysql_fetch_array($query)) {
// Write the output of each row
echo '<tr><td>'.$get["guest"].'</td><td>'.$get["company"].'</td></tr>';
}
// Close the table
echo '</table>';
If this is inside Wordpress, I would recommend using the $wpdb object for your query.