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
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'
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
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
I'm trying to write a custom select post query for wordpress. I'm using woocommerce. In woocommerce the default post type name is "product" and taxonomy name in "product_cat"
I've added another taxonomy names "spec" for product specification.
So in my product category assume "samsung", "laptop" is a term name for product category. and in "spec" I've some specification such as its "core i5 processor", "8gb ddr3 ram" etc. and my post title is "super notebook"
I want to search it for any of this field like if type notebook in my search field it will show all notebook. Underneath the search input type text area I also have a select box which has all "product_cat" there like samsung, hp, apple, laptop etc.
Product title / Post title
Super notebook
product_cat
samsung
laptop
spec
core i5 processor
8gb ddr3 ram
global $wpdb;
$query = "SELECT DISTINCT wp_posts.*
FROM wp_posts, wp_term_relationships, wp_term_taxonomy, wp_terms
WHERE (wp_terms.name LIKE '%ddr3 ram%'
OR wp_posts.post_title LIKE '%ddr3 ram%')
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'product'
AND wp_posts.ID = wp_term_relationships.object_id
AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.term_id = wp_terms.term_id GROUP BY wp_posts.ID ORDER BY wp_posts.post_title ASC";
$object = $wpdb->get_results($query);
_log($object);
I try this code it show the result.
But if I try this it'll not show the result
global $wpdb;
$query = "SELECT DISTINCT wp_posts.*
FROM wp_posts, wp_term_relationships, wp_term_taxonomy, wp_terms
WHERE (wp_terms.name LIKE '%ddr3 ram%'
AND wp_terms.name LIKE '%samsung%'
OR wp_posts.post_title LIKE '%ddr3 ram%')
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'product'
AND wp_posts.ID = wp_term_relationships.object_id
AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.term_id = wp_terms.term_id GROUP BY wp_posts.ID ORDER BY wp_posts.post_title ASC";
$object = $wpdb->get_results($query);
_log($object);
I don't have any idea to fix this. So anyone want to help me please give this answer for me.
Thanks.
You can try this
global $wpdb;
$query = "SELECT DISTINCT wp_posts.*
FROM wp_posts, wp_term_relationships, wp_term_taxonomy, wp_terms
WHERE (wp_terms.name LIKE '%ddr3 ram%'
OR wp_terms.name LIKE '%samsung%'
OR wp_posts.post_title LIKE '%ddr3 ram%')
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'product'
AND wp_posts.ID = wp_term_relationships.object_id
AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.term_id = wp_terms.term_id GROUP BY wp_posts.ID ORDER BY wp_posts.post_title ASC";
$object = $wpdb->get_results($query);
_log($object);