Wordpress query for posts is pulling up revisions as results - php

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' ....

Related

How to make comlex query to Wordpress database

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}';

WordPress Posts, order by custom table results

I have a blog that I need to re-order based on the number of values in a completely custom table. The reason I am not using meta data is a bit complex, but this is just what I need to do.
I just need to count the number of rows in the table wp_upvotes which have a postID that matches the ID of the WordPress blog post, and order it by most "upvotes" to least. This result should include the WordPress post even if there are no values in the wp_upvotes table.
The query I am trying is this:
$post = $wpdb->get_results("
SELECT wp_posts.*, COUNT(wp_upvotes.id) AS upvotes
FROM wp_posts
LEFT JOIN wp_upvotes
ON wp_posts.ID = wp_upvotes.postID
WHERE wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
ORDER BY upvotes DESC, wp_posts.date DESC
LIMIT $pageNum, $numToLoad
", ARRAY_A);
If you want to ensure that there is a match between each table, you are correct to use a LEFT JOIN - an easy way to think about it is that everything on the "left" (wp_posts) will be included, and things on the "right" (wp_upvotes) will be included if they have a match, otherwise null. Just using JOIN will ensure that rows from both tables will only be shown if there is a match.
My guess is that you need to include a GROUP BY p.ID to have each upvotes value specific to a particular post.
As a note, you also have an error using wp_posts.date instead of wp_posts.post_date;
It's also a good idea to use the $wpdb-posts and $wpdb-prefix properties in case you want to use this somewhere with a database prefix that is not wp_.
If you just want to see the results of the data in wp_posts you can just run a database query with an ORDER BY and return the columns, or if you want to use the WordPress filters (on things like the_title() and the_content() you can pass the post IDs into a WP_Query with the post_id__in and orderby=post__in arguments - you would need to reference back the $upvotes value by ID however.
global $wpdb;
$sql = <<<SQL;
SELECT p.*, COUNT(v.id) as upvotes
FROM {$wpdb->posts} p
JOIN {$wpdb->prefix}upvotes v
ON p.ID = v.postID
WHERE
p.posts_status = 'publish'
AND p.post_type = 'post'
GROUP BY p.ID
ORDER BY upvotes DESC, p.post_date DESC
LIMIT $pageNum, $numToLoad
SQL;
// use ARRAY_A to access as $row['column'] instead of $row->column
$rows = $wpdb->get_results( $sql );
foreach ( $rows as $row ){
$content = $row->post_content;
$upvotes = $row->upvotes;
}

Using LIMIT in MySQL to limit results based on column value (PHP/MySQL)

I did multiple searches for an answer to this (on SO and elsewhere), but haven't found an answer that really fit my needs (if it's out there, I apologize in advance).
I have a query, using PHP, that returns an array from a database (WordPress). Basically what I want to do is look at a column's value, and then LIMIT based on that value. Here's the array that's returned for a better idea:
http://pastebin.com/AC043qfh
In the query, you'll notice that the value for post_parent repeats for several of returned arrays. What I want to do is have it LIMIT to 3 based on the post_parent value e.g. I want 3 entries for post_parent 79, 87, 100, etc.
I'm not well versed (see: at all) in MySQL queries, but this is what I have to get that array:
SELECT DISTINCT ID, guid, post_parent, post_title
FROM $wpdb->posts p
WHERE p.post_type = 'attachment'
AND p.post_mime_type LIKE 'image/%'
AND p.post_status = 'inherit'
AND p.post_parent IN
(SELECT object_id FROM $term_relationships WHERE term_taxonomy_id = $post_term)
I've tried using GROUP BY, but that didn't get me what I wanted. Any help is appreciated.
EDIT Just to clarify, these are the results I want:
http://pastebin.com/pWXdUuXv
This might do the trick:
(I'm assuming ID is unique, if not substitute something that is)
SELECT
p.ID, guid, post_parent, post_title
FROM (
SELECT
a.ID as ID,
COUNT(*) as rank
FROM (
SELECT ID, post_parent
FROM $wpdb->posts
WHERE post_type = 'attachment'
AND post_mime_type LIKE 'image/%'
AND post_status = 'inherit'
) AS a
JOIN (
SELECT ID, post_parent
FROM $wpdb->posts
WHERE post_type = 'attachment'
AND post_mime_type LIKE 'image/%'
AND post_status = 'inherit'
) AS b ON b.ID <= a.ID AND b.post_parent = a.post_parent
GROUP BY a.ID
) AS r
JOIN $wpdb->posts p ON r.ID = p.ID AND r.rank <= 3
WHERE p.post_parent IN (
SELECT object_id FROM $term_relationships
WHERE term_taxonomy_id = $post_term)
GROUP BY p.ID
;
EDIT: Attempt to include category in rank so it'll actually work.
Specifying conditions twice is a bit ugly, but I didn't see an easy way around it.

Pull post from a specific Wordpress category

im looking to pull 3 posts from a specific Wordpress Category. At the minute I can pull 3 latest posts and display them in a flash banner using the code below.
SELECT yah_posts.*, yah_postmeta.*
FROM yah_posts
LEFT JOIN yah_postmeta ON yah_posts.ID = yah_postmeta.post_id
WHERE yah_postmeta.meta_key = 'largeimage' && yah_posts.post_status = 'publish'
ORDER BY post_date DESC LIMIT 3
I want to be able to pull 3 latest posts from a specific category instead of just 3 latest posts from every category.
I have put together this code below, but it doesn't seem to be working :(
$query = "SELECT yah_posts.*, yah_postmeta.*
FROM yah_posts
LEFT JOIN yah_postmeta ON yah_posts.ID = yah_postmeta.post_id
AND LEFT JOIN $yah_term_taxonomy ON($yah_term_relationships.term_taxonomy_id = $yah_term_taxonomy.term_taxonomy_id)
WHERE yah_postmeta.meta_key = 'largeimage' && yah_posts.post_status = 'publish'
AND $yah_term_taxonomy.term_id = '1'
AND $yah_term_taxonomy.taxonomy = 'category'
ORDER BY post_date DESC LIMIT 3";
There is actually a stack exchange site just focused on wordpress questions. Looking there and taking code from this question:
https://wordpress.stackexchange.com/questions/6533/modify-wordpress-sql-query-to-pull-from-within-a-category
You would need to add another JOIN:
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
And a couple more statements in your WHERE clause:
AND $wpdb->term_taxonomy.term_id = {term_id of the category you want}
AND $wpdb->term_taxonomy.taxonomy = 'category'
You can use wp_term_relationships in your query, take a look at wp_terms and wp_term_relationships tables in your database.

User's Post count from specific category [Wordpress]

I want to show user's post count from specific category. Currently, I can only be able to query all posts. My code is like this
<?php $userpost_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type ='post' AND post_author = '".$curauth->ID."'");?>
<?php echo "<span>Total post: </b></span>".$userpost_count.""?>
I know that, I need to join two table which is post table and term_relationships, but i don't know how to get it. Please kindly help me with that. Thank you.
Use your CATEGORY_ID below and try this:
<?php $userpost_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->term_taxonomy.term_id = CATEGORY_ID
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND post_author = '".$curauth->ID."'");?>

Categories