i used the following query to fetch the tittle,data,tagss content,slug,parent id .
this work fine but i need to get the post image and source of image that have a relation ship with wp_postmeta table so i used the second query in while loop inside of first query
First query:-
SELECT wp_posts.ID as Id,wp_posts.post_title as Title,wp_posts.post_date as DATE,
GROUP_CONCAT(wp_terms.name) AS TAGS, wp_posts.post_content as CONTENT,wp_terms.term_id,wp_terms.slug,wp_posts.post_parent as parent_id FROM wp_terms
INNER JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
INNER JOIN wp_posts ON wp_posts.ID = wp_term_relationships.object_id
WHERE post_type LIKE 'post' AND post_status LIKE 'publish' AND wp_terms.slug in ('interview','variety','lastpage','sport','arab-global','opinion','business','uae','firstpage')
GROUP BY wp_posts.ID order by post_date limit 150
and the sencond query as
select select wp_postmeta.meta_value from wp_postmeta left join wp_posts on wp_postmeta.post_id in ('".$row['Id']."') = wp_posts.id in ('".$row['Id']."') and meta_key='_wp_attached_file'"
but i does not get any details so i make the relation ship with parent_id of wp_post table as the third query as instead of second query.
select select wp_postmeta.meta_value from wp_postmeta left join wp_posts on wp_postmeta.post_id in ('".$row['parent_id']."') = wp_posts.id in ('".$row['parent_id']."') and meta_key='_wp_attached_file'"
after that i used single query to fetch all records like as below
SELECT wp_posts.ID as Id,wp_posts.post_title as Title,wp_posts.post_date as DATE,wp_postmeta.meta_key,wp_postmeta.meta_value,
GROUP_CONCAT(wp_terms.name) AS TAGS, wp_posts.post_content as CONTENT,wp_terms.term_id,wp_terms.slug,wp_posts.post_parent as parent_id FROM wp_terms
INNER JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
INNER JOIN wp_posts ON wp_posts.ID = wp_term_relationships.object_id
INNER JOIN wp_postmeta ON(wp_posts.ID = wp_postmeta.post_id)
WHERE post_type LIKE 'post' AND post_status LIKE 'publish'
GROUP BY wp_posts.ID order by post_date limit 150
but in that query i got multiple meta_key and multiple meta value there is any useful solution for that so i can fetch wp_post details with its source and image by simple in my sql and php
Try this
$meta_value_id= "select meta_value from wp_postmeta where meta_key='_thumbnail_id' and post_id='".$row['Id']."'";
$query_run_meta= mysql_query($meta_value_id);
$row1= mysql_fetch_assoc($query_run_meta);
$meta_image= "select meta_value from wp_postmeta where meta_key='_wp_attached_file' and post_id='".$row1['meta_value']."'";
$query_meta= mysql_query($meta_image);
$row2= mysql_fetch_assoc($query_meta);
now you can get image source from $row2.
From above query you are getting post id, you can use following functions to get post image url:
$post_thumbnail_id = get_post_thumbnail_id( $post_id ); //It will return id of attached thumbnail
$post_image_src= wp_get_attachment_image_src(post_thumbnail_id);// This will return source url of thumbnail
Related
How to get all posts from DB, with Category, Tags if any, feature_image and other related to post, in one row.
I have this one that gets attachment
SELECT
wp_posts.ID,
wp_posts.post_title AS title,
wp_posts.post_date AS published_at,
wp_posts.post_content AS content,
wp_posts.post_name AS slug,
files.meta_value AS foto
FROM `wp_posts`
INNER JOIN wp_posts attachments ON wp_posts.ID = attachments.post_parent
INNER JOIN wp_postmeta files ON attachments.ID = files.post_id
WHERE files.meta_key = '_wp_attached_file'
After I digged more I came up with this query :
SELECT
wp_posts.ID,
wp_terms.name as category,
wp_posts.post_title AS title,
wp_posts.post_date AS published_at,
wp_posts.post_content AS content,
wp_posts.post_name AS slug,
files.meta_value AS foto
FROM
`wp_posts`
LEFT JOIN wp_term_relationships ON(wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON(wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
LEFT JOIN wp_terms ON(wp_term_taxonomy.term_id = wp_terms.term_id)
INNER JOIN wp_posts attachments ON wp_posts.ID = attachments.post_parent
INNER JOIN wp_postmeta files ON attachments.ID = files.post_id
WHERE files.meta_key = '_wp_attached_file' AND wp_term_taxonomy.taxonomy = 'category' AND wp_posts.post_type = 'post'
I have a query that connects to a wordpress db and returns some posts.
In order to get the image for each of these posts i run another query inside a php foreach loop
The problem is that running the second query inside the foreach loop is extremely slow and i need another way to merge these 2 queries into 1.
The first query
SELECT pm. * , p.*
FROM wp_posts p
JOIN wp_postmeta pm ON pm.post_id = p.ID
WHERE pm.meta_key = 'accommodation_location_post_id'
AND pm.meta_value
IN (
SELECT pi.ID
FROM wp_posts pi
WHERE pi.post_title LIKE '%Cyprus%')
Based on the returned post ids, i need for each of these ids
the featured image.
This query does this job, but only for 1 id only.
The second query
SELECT wp_posts.guid
FROM wp_posts
WHERE wp_posts.ID =
(Select wp_postmeta.meta_value
FROM wp_postmeta
WHERE wp_postmeta.meta_key =
'_thumbnail_id' AND wp_postmeta.post_id = 'The id of each post')
I need a query to return all the posts alongside with its images.
I really don't understand your data structure, but try this query
select
t1.*, t2.guid
from (
SELECT pm. * , p.*
FROM wp_posts p
JOIN wp_postmeta pm ON pm.post_id = p.ID
WHERE pm.meta_key = 'accommodation_location_post_id'
AND pm.meta_value IN (
SELECT pi.ID
FROM wp_posts pi
WHERE pi.post_title LIKE '%Cyprus%'
)
) t1
left join (
select
p2.guid, pm2.post_id
from
wp_posts p2
join wp_postmeta pm2 on
pm2.meta_value = p2.ID
and pm2.meta_key = '_thumbnail_id'
) t2 on t2.post_id = t1.ID
I have created a custom post type and taxonomy for a goods catalogue for a hire website. This works fine. I am trying to generate a report that lists all items and their category and sub category. I came up with this to generate a table with the main category:
SELECT
wp_posts.ID as ID,
wp_posts.post_title as post_title,
wp_terms.name as main_category,
wp_terms.term_id as main_category_id
FROM
wp_posts, wp_term_relationships, wp_terms, wp_term_taxonomy
WHERE
wp_term_relationships.object_id = wp_posts.ID
AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.term_id = wp_terms.term_id
AND wp_term_taxonomy.parent = '0'
AND wp_posts.post_type = 'goods'
AND wp_posts.post_status = 'publish'
ORDER BY wp_terms.term_id ASC
I just loop through and its fine. But I want to show the sub category too - which I can do by putting another query in the foreach php loop.
BUT, I need to order by main category and then sub category.
So I figured I need it all in one query. And it must be possible. I have tried experimenting with JOINing tables in the query but can't get the syntax right. Heres the latest one I tried.
SELECT
wp_posts.ID as ID,
wp_posts.post_title as post_title,
s1.name as main_category,
s1.term_id as main_category_id,
s2.name as main_category,
s2.term_id as main_category_id
FROM
wp_posts, wp_term_relationships, wp_terms s1, wp_term_taxonomy
LEFT JOIN wp_terms s2
ON s1.term_id = s2.id
WHERE
wp_term_relationships.object_id = wp_posts.ID
AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.term_id = s1.term_id
AND wp_term_taxonomy.parent = '0'
AND wp_posts.post_type = 'goods'
AND wp_posts.post_status = 'publish'
ORDER BY s1.term_id ASC
Okay, trying to pull off some ninja development here, but getting a little stuck. I require the help of a true expert here. Thanks in advance.
This Query works excellent to return post information and category from outside of Wordpress:
SELECT wp_posts.ID,wp_posts.post_title,wp_terms.name as category FROM wp_posts
INNER JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id
INNER JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
INNER JOIN wp_terms ON wp_terms.term_id = wp_term_taxonomy.term_id
WHERE wp_term_taxonomy.taxonomy = 'category'
AND wp_posts.post_status = 'publish'
Now, this query works great for filtering by the company name, which is a meta field stored in the postmeta table. (The {$q} below is the sanitized company name)
SELECT ID,post_title,post_date,post_name,post_author FROM wp_posts,wp_postmeta
WHERE ID = wp_postmeta.post_id AND meta_key = 'company'
AND meta_value = \"{$q}\" AND wp_posts.post_status = 'publish'
ORDER BY post_date DESC
Anyway, both queries work great on their own. However, I want to combine them into one query so that I can select the category but still filter by company.
Any thoughts? I would love to hear your ideas on how to solve this problem. I'd give you 3x points for this answer if I could.
One way to accomplish this:
SELECT wp_posts.ID,wp_posts.post_title,wp_terms.name as vert FROM wp_posts
INNER JOIN wp_term_relationships on wp_posts.ID = wp_term_relationships.object_id
INNER JOIN wp_term_taxonomy on wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
INNER JOIN wp_terms ON wp_terms.term_id = wp_term_taxonomy.term_id
WHERE wp_term_taxonomy.taxonomy = 'category'
AND wp_posts.post_status = 'publish'
AND wp_posts.ID IN (SELECT post_id FROM wp_postmeta WHERE meta_key = 'company' AND meta_value = \"{$q}\" ) LIMIT 0,99999
I have some regular WordPress posts that I need to get from the database by parent category, and order by the results of a custom table in the database that I have made.
The parent category that I want to retrieve posts for (including children) is called explorer with the id of 29.
The custom table is called wp_upvotes. in this table there are a few columns, but the only columns that we care about would probably be id and postID. I want to order the wp_posts by the number of rows that has the postID equal to the wp_post.ID, and if there are no rows in that table for the other posts, then they should be ordered by date at the end. I want the most number of upvotes to the least number of upvotes by date.
The query I have tried is this (it returns only the first post, not all of them):
$catIDs = array(29,30,31,32);
SELECT wp_posts.*, COUNT(wp_upvotes.id) AS upvotes FROM wp_posts
LEFT JOIN wp_upvotes ON (wp_posts.ID = wp_upvotes.postID)
INNER JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_taxonomy
ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.taxonomy = 'category'
AND wp_term_taxonomy.term_id IN (" . implode(',', $catIDs) . "))
AND wp_posts.post_status = 'publish'
ORDER BY upvotes DESC, wp_posts.post_date DESC
When I remove the LEFT JOIN for the wp_upvotes table it returns all of the correct posts.. Why is it only returning one row when I am using a LEFT JOIN?
SELECT wp_posts.* FROM wp_posts
INNER JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_taxonomy
ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.taxonomy = 'category'
AND wp_term_taxonomy.term_id IN (" . implode(',', $catIDs) . "))
AND wp_posts.post_status = 'publish'
ORDER BY wp_posts.post_date DESC
Looks like the main factor was to use ORDER BY wp_posts.ID for whatever reason to make it show all of the rows instead of just one.. Odd but here's my final code to do what I wanted to do above:
SELECT wp_posts.*, COUNT(wp_upvotes.id) AS upvotes FROM wp_posts
INNER JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_taxonomy
ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.taxonomy = 'category'
AND wp_term_taxonomy.parent = 29)
LEFT JOIN wp_upvotes ON (wp_posts.ID = wp_upvotes.postID)
WHERE wp_posts.post_status = 'publish'
GROUP BY wp_posts.ID
ORDER BY upvotes DESC, wp_posts.post_date DESC