With this query i get the posts i need from my wordpress db. What i have to do to get the posts from a specific category?
$sql = "select DISTINCT wp_posts.id, wp_posts.post_title, wp_posts.post_excerpt, wp_posts.guid, wp_posts.post_type, featured_image.guid as post_image, wp_posts.post_modified, wp_users.display_name
from wp_posts
inner join wp_postmeta on wp_posts.id = wp_postmeta.post_id and wp_postmeta.meta_key = '_thumbnail_id'
inner join wp_posts as featured_image on featured_image.id = wp_postmeta.meta_value
inner join wp_users on wp_users.id = wp_posts.post_author
where wp_posts.post_status = 'publish'
AND DATE_FORMAT(wp_posts.post_date, '%d-%m-%Y')='$mydate'
order by wp_posts.ID desc
limit 20";
You have to have joins with three more tables: wp_term_relationships, wp_term_taxonomy and wp_terms, and in the where clause you can search for the category name from the name column of the terms table. Have updated the sql query
$sql = "select DISTINCT wp_posts.id, wp_posts.post_title, wp_posts.post_excerpt, wp_posts.guid, wp_posts.post_type, featured_image.guid as post_image, wp_posts.post_modified, wp_users.display_name
from wp_posts
inner join wp_postmeta on wp_posts.id = wp_postmeta.post_id and wp_postmeta.meta_key = '_thumbnail_id'
inner join wp_posts as featured_image on featured_image.id = wp_postmeta.meta_value
inner join wp_users on wp_users.id = wp_posts.post_author
INNER JOIN wp_term_relationships rel ON wp_posts.ID = rel.object_id
INNER JOIN wp_term_taxonomy taxonomy ON rel.term_taxonomy_id = taxonomy.term_taxonomy_id
INNER JOIN wp_terms terms ON taxonomy.term_id = terms.term_id
where wp_posts.post_status = 'publish'
AND terms.name = 'Category you wist to search for'
AND DATE_FORMAT(wp_posts.post_date, '%d-%m-%Y')='$mydate'
order by wp_posts.ID desc
limit 20";
Related
I have a query that is working to display rows who contain specific meta key.
select wp_woocommerce_order_itemmeta.*
from wp_posts, wp_postmeta
inner join wp_woocommerce_order_items, wp_woocommerce_order_itemmeta
where wp_posts.post_type = "shop_subscription"
and wp_postmeta.post_id = wp_posts.ID
and wp_postmeta.meta_key = "_shipping_country"
and wp_postmeta.meta_value = "FR"
and wp_woocommerce_order_items.order_id = wp_posts.ID
and wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id
and wp_woocommerce_order_itemmeta.meta_key = "_subtracted_base_location_taxes"
I want to delete these rows but with a limit. I ve got an error with this sql
DELETE wp_woocommerce_order_itemmeta.*
FROM wp_posts, wp_postmeta
inner join wp_woocommerce_order_items, wp_woocommerce_order_itemmeta
WHERE wp_posts.post_type = "shop_subscription"
and wp_postmeta.post_id = wp_posts.ID
and wp_postmeta.meta_key = "_shipping_country"
and wp_postmeta.meta_value = "FR"
and wp_woocommerce_order_items.order_id = wp_posts.ID
and wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id
and wp_woocommerce_order_itemmeta.meta_key = "_subtracted_base_location_taxes"
LIMIT 100
I have read that you can't use LIMIT directly within DELETE when you're referencing multiple tables at the same time. I begin with mysql and i am stuck. Any help would be apreciate.
Thanks
try something like this
delete a
from wp_woocommerce_order_itemmeta a
join (
select wp_woocommerce_order_itemmeta.meta_id
from wp_posts, wp_postmeta
inner join wp_woocommerce_order_items, wp_woocommerce_order_itemmeta
where wp_posts.post_type = 'shop_subscription'
and wp_postmeta.post_id = wp_posts.ID
and wp_postmeta.meta_key = '_shipping_country'
and wp_postmeta.meta_value = 'FR'
and wp_woocommerce_order_items.order_id = wp_posts.ID
and wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id
and wp_woocommerce_order_itemmeta.meta_key = '_subtracted_base_location_taxes'
limit 100
) b on a.meta_id = b.meta_id
mysql delete with inner joins and limit
WordPress SQL Custom Query To Get Post, Post Meta Data, Category and Featured Image of the post.
I've tried so far the below code and got the post and post meta data. Now taxonomy and featured image remaining:
$query = mysql_query("
SELECT wp_posts.ID
, wp_posts.post_title
, mt1.meta_value as latitude
, mt2.meta_value as longitude
, mt3.meta_value as full_adddress
FROM wp_posts
LEFT JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id AND mt1.meta_key='lv_listing_lat')
LEFT JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id AND mt2.meta_key='lv_listing_lng')
LEFT JOIN wp_postmeta AS mt3 ON (wp_posts.ID = mt3.post_id AND mt3.meta_key='_address')
WHERE wp_posts.post_type = 'lv_listing'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
AND ((mt1.meta_key = 'lv_listing_lat') OR (mt2.meta_key = 'lv_listing_lng' ) OR (mt3.meta_key = '_address' ))
GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC ");
Revised code by myself. In this code category achieved, now only featured image attachment url needed.
SELECT DISTINCT
ID, post_title
, post_content
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'lv_listing_lat' AND wp_postmeta.post_id = wp_posts.ID) AS "lv_listing_lat"
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'lv_listing_lng' AND wp_postmeta.post_id = wp_posts.ID) AS "lv_listing_lng"
,(SELECT group_concat(wp_terms.name separator ', ')
FROM wp_terms
INNER JOIN wp_term_taxonomy on wp_terms.term_id = wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships wpr on wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE taxonomy= 'listing_category' and wp_posts.ID = wpr.object_id
) AS "Listing Category"
,(SELECT group_concat(wp_terms.name separator ', ')
FROM wp_terms
INNER JOIN wp_term_taxonomy on wp_terms.term_id = wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships wpr on wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE taxonomy= 'listing_location' and wp_posts.ID = wpr.object_id
) AS "Listing Location"
FROM wp_posts
WHERE post_type = 'lv_listing'
ORDER BY
post_title
, post_content
i wanna update thumbnails of all products in specific categories of my shop with one image.
I know the categories id and their term_taxonomy_id but I cannot do proper update statement with subquery ...
Here is my code..
UPDATE wp_postmeta
SET wp_postmeta.meta_value = '5898'
WHERE wp_postmeta.meta_key = '_thumbnail_id'
AND
wp_postmeta.posts_id = (
SELECT wp_posts.* FROM wp_term_relationships
LEFT JOIN wp_posts ON wp_term_relationships.object_id = wp_posts.ID
LEFT JOIN wp_term_taxonomy ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
LEFT JOIN wp_terms ON wp_terms.term_id = wp_term_relationships.term_taxonomy_id
WHERE post_type = 'product' AND taxonomy = 'product_cat'
AND wp_term_taxonomy.term_taxonomy_id = '130')
Where I make mistake ??
product category id 114 and in term_taxonomy_id 130
This part of the question works good but changes ALL products thumbanils not only from specific category
UPDATE wp_postmeta
SET wp_postmeta.meta_value = '5898'
WHERE wp_postmeta.meta_key = '_thumbnail_id'
Maybe I will ask for help not for resolving my problem in my way :)
This query gives some records
SELECT wp_posts.* FROM wp_term_relationships
LEFT JOIN wp_posts ON wp_term_relationships.object_id = wp_posts.ID
LEFT JOIN wp_term_taxonomy ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
LEFT JOIN wp_terms ON wp_terms.term_id = wp_term_relationships.term_taxonomy_id
WHERE post_type = 'product' AND taxonomy = 'product_cat'
AND wp_term_taxonomy.term_taxonomy_id = '130'
and I want to use ID (from results of the query above) as wp_postmeta.post_id to change some values in those records in wp_postmeta in the query below
UPDATE wp_postmeta
SET wp_postmeta.meta_value = '5898'
WHERE wp_postmeta.meta_key = '_thumbnail_id'
Anyone ?? Help plis....
I'm using this sql to return results based on an inner join with 3 meta values. It only seems to work with 1 AND ( ), when i add the other two it returns 0 results.
SELECT * FROM wp_posts
INNER JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE wp_posts.post_type = 'plot'
AND wp_posts.post_status = 'publish'
AND ( wp_postmeta.meta_key = 'plot_type' AND wp_postmeta.meta_value = 'Cottage' )
AND ( wp_postmeta.meta_key = 'number_of_bedrooms' AND wp_postmeta.meta_value = '2' )
AND ( wp_postmeta.meta_key = 'property' AND wp_postmeta.meta_value = '446' )
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_title ASC;
I think you meant to use OR with the other 2 (see below). The same field can't be 2 different things, which is why you get 0 results.
SELECT *
FROM wp_posts
INNER JOIN wp_postmeta
ON (wp_posts.ID = wp_postmeta.post_id)
WHERE wp_posts.post_type = 'plot'
AND wp_posts.post_status = 'publish'
AND ((wp_postmeta.meta_key = 'plot_type' AND
wp_postmeta.meta_value = 'Cottage') OR
(wp_postmeta.meta_key = 'number_of_bedrooms' AND
wp_postmeta.meta_value = '2') OR (wp_postmeta.meta_key = 'property' AND
wp_postmeta.meta_value = '446'))
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_title ASC;
edit, try below instead:
select * from wp_posts
join wp_postmeta on wp_posts.ID = wp_postmeta.post_id
where wp_posts.post_type = 'plot'
and wp_posts.post_status = 'publish'
and concat(wp_postmeta.meta_key,'|',wp_postmeta.meta_value)
in ('plot_type|Cottage',
'number_of_bedrooms|2',
'property|446');
You need to join the wp_postmeta table once for each type of value you need.
SELECT whatever, whatever
FROM wp_posts AS p
JOIN wp_postmeta AS plottype
ON (p.ID = plottype.post_id AND plottype.meta_key = 'plot_type')
JOIN wp_postmeta AS bedrooms
ON (p.ID = bedrooms.post_id AND bedrooms.meta_key = 'number_of_bedrooms')
JOIN wp_postmeta AS property
ON (p.ID = property.post_id AND property.meta_key = 'property')
WHERE wp_posts.post_type = 'plot'
AND wp_posts.post_status = 'publish'
AND plottype.meta_value = 'Cottage'
AND bedrooms.meta_value = '2'
AND property.meta_value = '466'
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_title ASC;
This wp_postmeta key/value storage is a little tricky to join to; your join criteria need to pull the appropriate key as well as the matching post ID.
It's well known that SELECT * is a bad idea in software. It's especially bad when you're joining so many tables. List the columns you need in your result set.
Notice also that you're using INNER JOIN with which JOIN is synonymous. If any of the values you're pulling from the metadata are missing, so will be the row from your result set. You may or may not be better off using LEFT JOINs (You didn't explain the purpose of the query.)
It seems as some of the structure for your conditions should be changed.
Try the following:
SELECT * FROM wp_posts
INNER JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE wp_posts.post_type = 'plot'
AND wp_posts.post_status = 'publish'
AND (
(wp_postmeta.meta_key = 'plot_type' AND wp_postmeta.meta_value = 'Cottage')
OR
(wp_postmeta.meta_key = 'number_of_bedrooms' AND wp_postmeta.meta_value = '2')
OR
(wp_postmeta.meta_key = 'property' AND wp_postmeta.meta_value = '446')
)
ORDER BY wp_posts.post_title ASC;
I managed to fix the issue using WP_Meta_Query, the SQL it produced was...
SELECT wp_posts.* FROM wp_posts
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id)
WHERE 1=1
AND wp_posts.post_type = 'plot'
AND (wp_posts.post_status = 'publish')
AND (
(wp_postmeta.meta_key = 'property' AND CAST(wp_postmeta.meta_value AS CHAR) = '180')
AND (mt1.meta_key = 'plot_type' AND CAST(mt1.meta_value AS CHAR) = 'Cottage')
AND (mt2.meta_key = 'number_of_bedrooms' AND CAST(mt2.meta_value AS CHAR) = '2')
)
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_title ASC;
Thanks to everyone for the help :)
First I would like to mention I have limited skills when it comes to MYSQL an JOIN. However this is what I have and what I like to achieve:
I have the default WordPress tables and like to get a result with post_name, title, status and the meta_value from a certain meta key.
This is what I have:
SELECT
wp_posts.ID, wp_posts.post_name, wp_posts.post_title, wp_posts.post_status, wp_postmeta.meta_value
FROM wp_posts
INNER JOIN
wp_term_relationships ON ( wp_posts.ID = wp_term_relationships.object_id )
INNER JOIN
wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE (
wp_term_relationships.term_taxonomy_id
IN ( 1, 2, 3 )
)
AND wp_posts.post_type = 'my_post_type'
AND (
wp_posts.post_status
IN (
'my_status_1', 'my_status_2'
)
)
AND wp_postmeta.meta_key = 'my_meta_key'
GROUP BY wp_posts.ID
ORDER BY wp_posts.ID ASC
Everything works as expected when each post has a postmeta of 'my_meta_key'. But if the 'my_meta_key' is missing the post is not in the result.
I guess it' caused by the second INNER JOIN but as mentioned I have no idea what I should it replace it with.
I'm sure it's something simple
Move the AND wp_postmeta.meta_key = 'my_meta_key' to
INNER JOIN
wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
Like this and change the INNER for a LEFT
LEFT JOIN
wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id
AND wp_postmeta.meta_key = 'my_meta_key')
If you put your condition in the "Where Clause" The Left join will be "overwritten".
And I will add an advice. Dont use parenthesis when you dont really need it to keep your code easy to read.
The following returns
all records from WP_Posts
only those records with WP_TERM_RELATIONSHIPS
only those records in wp_postmeta which a matching record in wp_posts.
*
SELECT wp_posts.ID, wp_posts.post_name, wp_posts.post_title,
wp_posts.post_status, wp_postmeta.meta_value
FROM wp_posts
LEFT JOIN wp_term_relationships
ON wp_posts.ID = wp_term_relationships.object_id
LEFT JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_term_relationships.term_taxonomy_id IN ( 1, 2, 3 )
AND wp_posts.post_type = 'my_post_type'
AND wp_posts.post_statusIN ('my_status_1', 'my_status_2')
AND (wp_postmeta.meta_key = 'my_meta_key' or wp_postmeta.meta_key is null)
GROUP BY wp_posts.ID
ORDER BY wp_posts.ID ASC
you need the is null otherwise records in wp_posts without wp_postmeta data will be excluded.
Change the INNER JOIN on the wp_postmeta table to a LEFT JOIN:
LEFT JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
And move the WHERE filter for the wp_postmeta to the JOIN condition.
So your query will be:
SELECT wp_posts.ID, wp_posts.post_name, wp_posts.post_title, wp_posts.post_status, wp_postmeta.meta_value
FROM wp_posts
INNER JOIN wp_term_relationships
ON ( wp_posts.ID = wp_term_relationships.object_id )
LEFT JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
AND wp_postmeta.meta_key = 'my_meta_key'
WHERE wp_term_relationships.term_taxonomy_id IN ( 1, 2, 3 )
AND wp_posts.post_type = 'my_post_type'
AND wp_posts.post_status IN ('my_status_1', 'my_status_2')
GROUP BY wp_posts.ID
ORDER BY wp_posts.ID ASC
The INNER JOIN syntax will only return rows that match in both tables. So if you do not have a matching row, you will not get any result. By changing that to a LEFT JOIN, you will return all rows even if there is not a matching row in the wp_postmeta table. If the row does not exist, then the values from the wp_postmeta table will be null.