WordPress posts with featured images and categories - mysql

I am looking for a solution for:
SELECT ID
, post_date_gmt
, post_content
, post_title
, post_modified_gmt
FROM wphj_posts
WHERE post_status = 'publish'
and:
SELECT DISTINCT meta_value
, post_id
, meta_id
FROM wphj_postmeta
WHERE meta_key LIKE '_wp_attached_file'
ORDER
BY meta_value ASC
What I did first was this, which doesn't work as I want it to:
SELECT ID
, post_date_gmt
, post_content
, post_title
, post_modified_gmt
, meta_value
FROM wphj_posts
, wphj_postmeta
WHERE post_status = 'publish'
AND meta_key LIKE '_wp_attached_file'
Am simply trying to query WordPress database to get post data from wp_posts and the featured image of the same post from the wp_postmeta table.
Stuff I tried to do: used union, unionall, join on, inner join, left join. Unfortunately they don't work.
Update, came up with this using wpDatatables plugin, works on the plugin dashboard but not as a query on its own :\ - what am i doing wrong folks
SELECT posts_post.ID AS post_ID,
posts_post.post_date_gmt AS post_post_date_gmt,
posts_post.post_title AS post_post_title,
CONCAT('',posts_post.post_title,'') AS post_title_with_link_to_post,
posts_post.post_content AS post_post_content,
posts_post.post_status AS post_post_status,
posts_post.post_name AS post_post_name,
CONCAT(
'<a href="',
posts_post.guid,
'"><img src="',
REPLACE(
posts_post_img.guid,
CONCAT(
'.',
SUBSTRING_INDEX(
posts_post_img.guid,
'.',
-1
)
),
CONCAT(
'-150x150.' ,
SUBSTRING_INDEX(
posts_post_img.guid,
'.',
-1
)
)
),
'" /></a>'
) AS post_thumbnail_with_link_to_post,
post_taxonomy_category_tbl.name AS post_taxonomy_category
FROM wphj_posts AS posts_post
INNER JOIN (SELECT name, object_id as id FROM wphj_terms AS post_taxonomy_category_tbl_terms INNER JOIN wphj_term_taxonomy AS post_taxonomy_category_tbl_termtaxonomy ON post_taxonomy_category_tbl_termtaxonomy.term_id = post_taxonomy_category_tbl_terms.term_id AND post_taxonomy_category_tbl_termtaxonomy.taxonomy = 'category' INNER JOIN wphj_term_relationships AS rel_post_taxonomy_category_tbl ON post_taxonomy_category_tbl_termtaxonomy.term_taxonomy_id = rel_post_taxonomy_category_tbl.term_taxonomy_id) AS post_taxonomy_category_tbl
ON post_taxonomy_category_tbl.ID = posts_post.id
LEFT JOIN (SELECT posts_post_imgposts.guid AS guid, posts_post_imgpostmeta.post_id AS post_id
FROM wphj_postmeta AS posts_post_imgpostmeta
INNER JOIN wphj_posts AS posts_post_imgposts
ON posts_post_imgpostmeta.meta_value = posts_post_imgposts.ID
WHERE posts_post_imgpostmeta.meta_key = '_thumbnail_id' ) AS posts_post_img
ON posts_post_img.post_id = posts_post.ID
WHERE 1=1
AND posts_post.post_type = 'post'

You can try this code, I ran it in sequal pro on on of my local installations, and I got the it to work as I think you would like it to work.
SELECT ID,
post_date_gmt,
post_content,
post_title,
post_modified_gmt,
meta_value
FROM wphj_posts, wphj_postmeta
WHERE wphj_posts.ID = wphj_postmeta.post_id
AND wphj_postmeta.meta_key = '_wp_attached_file'
AND wphj_posts.post_status = 'publish'

Related

Wordpress get attachment url using wpdb

How can I modify this query to get the attachment url link to my custom post type.
I have tried several solution from the web but none of them are giving the expected result.
$results = $wpdb->get_results(
"SELECT ID, post_title, {$wpdb->prefix}terms.name, GROUP_CONCAT(meta_value SEPARATOR ', ') AS pictoInfos
FROM {$wpdb->prefix}posts
INNER JOIN {$wpdb->prefix}term_relationships
ON ID = {$wpdb->prefix}term_relationships.object_id
INNER JOIN {$wpdb->prefix}terms
ON {$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}terms.term_id
INNER JOIN {$wpdb->prefix}postmeta
ON ID = {$wpdb->prefix}postmeta.post_id
WHERE (post_type = 'recette' AND post_status = 'publish')
AND (term_taxonomy_id = {$_GET['data']['id']} AND meta_key IN ( 'kilocalorie', 'difficulte', 'nombre_de_minutes' ) )
GROUP BY ID ORDER BY ID DESC ",
ARRAY_A );

Multiple Meta_Key Select on on Wordpress database

I've developed a query which selects events from Wordpress. I am using the where clause to select where the meta_value of the meta_key eventstartdate is after today.
The issue I'm having now is, I also want filter on a second meta_value that being from the meta_key '_VenueCity'.
I have tried aliasing the wp_postmeta table and doing a where on the meta_key but I think I'm missing a join.
This is the code that works without my additional code to get it to work. Can any one advise on how I get this to work?
SELECT
`wp_posts`.`ID` AS `EventID`,
`wp_posts`.`post_parent` AS `SeriesID`,
`wp_posts`.`post_title` AS `EventTitle`,
`wp_posts`.`post_content` AS `EventDescription`,
`wp_posts`.`post_excerpt` AS `EventSummary`,
`wp_posts`.`post_name` AS `EventSlug`,
min(`wp_postmeta`.`meta_value`) AS `EventStartDate`,
max(`tribe_event_end_date`.`meta_value`) AS `EventEndDate`,
`wp_posts`.`guid` AS `GUID`
FROM ((`wp_posts`
JOIN `wp_postmeta` ON
(
(`wp_posts`.`ID` = `wp_postmeta`.`post_id`)
))
LEFT JOIN `wp_postmeta` `tribe_event_end_date` ON
(
(
(`wp_posts`.`ID` = `tribe_event_end_date`.`post_id`) AND
(`tribe_event_end_date`.`meta_key` = '_EventEndDate')
)
))
WHERE
(
(`wp_postmeta`.`meta_key` = '_EventStartDate') AND
(`wp_posts`.`post_type` = 'tribe_events') AND
(`wp_posts`.`post_status` = 'publish') AND
(`tribe_event_end_date`.`meta_value` >= CURDATE())
)
GROUP BY
`wp_posts`.`ID`
ORDER BY
`EventStartDate`,
`wp_posts`.`post_date`;
I am not going to write your query for you but I will give an example of how to get multiple postmeta values. The power will be in the where clause to get the right values.
You should also consider what joins you want to use.
SELECT
p.post_title,
pm1.meta_value,
pm2.meta_value
FROM wp_posts as p
INNER JOIN wp_postmeta as pm1
ON p.ID = pm1.post_id
INNER JOIN wp_postmeta as pm2
ON p.ID = pm2.post_id
WHERE
pm1.meta_key = '_my_postmeta_field1'
AND
pm2.meta_key <> '_not_this_field'

Combining 3 different queries

I'm querying a MySQL db for a report in SSRS 2008, and I have no problem using the following to join two different queries...
SELECT a.arrivalDate, a.order_item_id, b.roomType
FROM
(select order_item_id, meta_value as arrivalDate from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'Arrival Date') as a,
(select order_item_id, meta_value as roomType from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'type-of-room') as b
WHERE a.order_item_id = b.order_item_id
...but when I try to add a third, it's taking issue with my WHERE clause, but I coulda swore I used to do it this way:
SELECT a.arrivalDate, a.order_item_id, b.roomType, c.retreatDate
FROM
(select order_item_id, meta_value as arrivalDate from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'Arrival Date') as a,
(select order_item_id, meta_value as roomType from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'type-of-room') as b,
(select order_item_id, meta_value as retreatDate from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'retreat-date') as c,
WHERE a.order_item_id = b.order_item_id AND a.order_item_id = c.order_item_id
Any advice?
Never mind, got it! I thought this is what I tried originally, but I must've had a typo. Hopefully, my error will help others stumbling across this in the future:
SELECT a.arrivalDate, a.order_item_id, b.roomType, c.retreatDate
FROM
(select order_item_id, meta_value as arrivalDate from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'Arrival Date') as a,
(select order_item_id, meta_value as roomType from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'type-of-room') as b,
(select order_item_id, meta_value as retreatDate from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'retreat-date') as c
where a.order_item_id = b.order_item_id AND b.order_item_id = c.order_item_id
I would stick with either of the conventional solutions - the first is (IMO) easier to read. The second is typically faster...
Query 1
SELECT order_item_id
, MAX(CASE WHEN meta_key = 'Arrival Date' THEN meta_value END) arrivalDate
, MAX(CASE WHEN meta_key = 'type-of-room' THEN meta_value END) roomType
, MAX(CASE WHEN meta_key = 'retreat-date' THEN meta_value END) retreatDate
FROM wp_woocommerce_order_itemmeta
GROUP
BY order_item_id
Query 2
SELECT DISTINCT x.order_item_id
, arrival.meta_value arrivalDate
, room.meta_value roomType
, date.meta_value retreatDate
FROM wp_woocommerce_order_itemmeta x
LEFT
JOIN wp_woocommerce_order_itemmeta arrival
ON arrival.order_item_id = x.order_item_id
AND arrival.meta_key = 'Arrival Date'
LEFT
JOIN wp_woocommerce_order_itemmeta room
ON room.order_item_id = x.order_item_id
AND room.meta_key = 'type-of-room'
LEFT
JOIN wp_woocommerce_order_itemmeta date
ON date.order_item_id = x.order_item_id;

MySQL subquery returns more than one row and errors out

I have a SQL statement here I need rows that are within a certain column to be divided into rows... but when I retrieve the data is returns the error
1 Subquery returns more than 1 row
:( please help here is my query
(select
(SELECT
(meta_value)
FROM
main_postmeta
WHERE
meta_key = 'project_year') as Year,
(SELECT
(meta_value)
FROM
main_postmeta
WHERE
meta_key = 'project_gapp_city') as Location,
(SELECT
(meta_value)
FROM
main_postmeta
WHERE
meta_key = 'project_title') as Title
FROM
main_postmeta
WHERE
main_postmeta.post_id = '1423'
ORDER BY meta_value desc);
You seem to be looking for a pivoted result set.
select
case when meta_key='project_year' then meta_value else null end as `YEAR`
, case when meta_key='project_gapp_city' then meta_value else null end as `LOCATION`
, case when meta_key='project_title' then meta_value else null end as `Title`
from main_postmeta
where main_postmeta.post_id = '1423'
order by meta_value desc
If the values fetching are grouped under a single post_id then:
select
max ( case when meta_key='project_year'
then meta_value
else null
end ) as `YEAR`
, max ( case when meta_key='project_gapp_city'
then meta_value
else null
end ) as `LOCATION`
, max ( case when meta_key='project_title'
then meta_value
else null
end ) as `Title`
from main_postmeta
where main_postmeta.post_id = '1423'
order by meta_value desc
Probably best to do a join for each meta value, something like this:-
SELECT a.meta_value, b.meta_value, c.meta_value
FROM main_postmeta a
INNER JOIN main_postmeta b ON a.post_id = b.meta_key AND b.meta_key = 'project_gapp_city'
INNER JOIN main_postmeta c ON a.post_id = c.meta_key AND c.meta_key = 'project_title'
WHERE a.post_id = '1423'
AND a.meta_key = 'project_year'
ORDER BY meta_value DESC
Depending on what you want to do with the results you might also be able to do something like this:-
SELECT GROUP_CONCAT(CONCAT_WS('|', meta_key, meta_value))
FROM main_postmeta a
WHERE a.post_id = '1423'
AND a.meta_key IN ( 'project_gapp_city', 'project_year', 'project_title')
and then explode out the keys and values.

How to serialize C# data to wp_postmeta (MySQL longtext column)

I'm trying to write a SQL update that takes part of a string and updates with part of another string (both strings already in the database).
My query looks like this, but it doesn't work :(
UPDATE wp_postmeta SET meta_value = REPLACE(meta_value,
SELECT SUBSTRING(meta_value, 1, 23) AS meta_header
FROM wp_postmeta WHERE meta_id = 8443,
SELECT SUBSTRING(meta_value, 1, 23) AS meta_header
FROM wp_postmeta WHERE meta_id = 2037)
WHERE post_id = 8443
Any ideas for how to write it?
Thanks.
Does this work?
replace into wp_postmeta (meta_id,meta_value)
select 8443,concat(SUBSTRING(meta_value,1,23),t.end)
from wp_postmeta JOIN
(select SUBSTRING(meta_value,24) as end from wp_postmeta where meta_id=8443) as t
where meta_id=2037
Solution with group_concat:
replace into wp_postmeta (meta_id,meta_value)
select 8443,group_concat(type SEPARATOR '') from
(select mem_id,substring(type,1,23) as type from wp_postmeta where mem_id=2037
UNION
select mem_id,substring(type,24) as type from wp_postmeta where mem_id=8443) as t;
Try this by joining your tables
UPDATE wp_postmeta wp1
JOIN wp_postmeta wp2 ON (wp1.meta_id =wp2.meta_id )
JOIN wp_postmeta wp3 ON (wp1.meta_id =wp3.meta_id )
SET wp1.meta_value = REPLACE(wp1.meta_value, SUBSTRING(wp2.meta_value, 1, 23) ,SUBSTRING(wp3.meta_value, 1, 23))
WHERE wp2.meta_id = 8443 AND wp3.meta_id = 2037
AND wp1.post_id = 8443