query 2 wordpress database tables at once - wordpress

I have the following query which i am running:
$query = "SELECT post_title, ID, post_date_gmt, post_author
FROM wp_posts
WHERE post_type='course-manager'
AND post_status='publish'
AND SUBSTRING(post_date_gmt,1,4) = '$this->yearGet'
AND SUBSTRING(post_date_gmt,6,2) = '$this->monthGet'
AND post_author = '$userid'
ORDER BY post_title";
$query_result = mysql_query ($query);
while ($info = mysql_fetch_array($query_result))
{
currently this is querying just the wordpress posts table. i would like to also query data from the wordpress postmeta table, an example would be like this where i have added the "AND course_date which is within the postmeta table:
$query = "SELECT post_title, ID, post_date_gmt, post_author
FROM wp_posts
WHERE post_type='course-manager'
AND post_status='publish'
AND SUBSTRING(post_date_gmt,1,4) = '$this->yearGet'
AND SUBSTRING(post_date_gmt,6,2) = '$this->monthGet'
AND post_author = '$userid'
AND course_date = '$this->yearGet'
ORDER BY post_title";
$query_result = mysql_query ($query);
while ($info = mysql_fetch_array($query_result))
{
any help would be greatly appreciated

If you need to query a combined set of data whose source is from more than one table, you need to do a join. Basically, you need to make the connection between the 2 tables, in the post and postmeta case, that connection would be the post id. So the query should look like this:
SELECT a.something, b.anotherthing
FROM wp_posts AS a JOIN wp_postmeta AS b
ON a.ID = b.post_id
//followed by all of your where clause...

Related

Get meta_value from GDRating

I am trying to get meta value from GD Rating table for each post ID with following sql:
$querystr = "SELECT meta_value FROM $wpdb->gdrts_itemmeta WHERE meta_key LIKE 'stars-rating_rating'";
$ratings = $wpdb->get_results($querystr);
foreach ($ratings as $rating)
{
$rating->meta_value;
}
$ratingku = get_post_meta($post->ID, $rating, true);
But it failed. It return a word: "ARRAY".
How to get meta_value from another table (created by a plugin) for each post by using SQL or Query?
To get the rating value for the post use below query:
global $wpdb;
$querystr = "SELECT meta_value AS rating FROM ".$wpdb->prefix."gdrts_itemmeta INNER JOIN ".$wpdb->prefix."gdrts_items ON ".$wpdb->prefix."gdrts_items.item_id = ".$wpdb->prefix."gdrts_itemmeta.item_id AND ".$wpdb->prefix."gdrts_itemmeta.meta_key = 'stars-rating_rating' AND ".$wpdb->prefix."gdrts_items.id = ".$post->ID;
$result = $wpdb->get_row($querystr);
echo $result->rating;

Count number of posts have particular meta key or value?

What I want to achieve is display the number of posts which have particular meta key or value I am getting a list of posts and meta key and value but don't know how to display them I'm storing data using repeatable fields. Storing work properly.
Now, for example, I have age meta value in two posts so how can I count no of a post with age. Age = No of post 2.
My Code :
global $wpdb;
$query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}postmeta WHERE (meta_key = 'repeatable_fields') ");
$array = wp_json_encode($query);
print_r($array);
Outout :
[{"meta_id":"312","post_id":"108","meta_key":"repeatable_fields","meta_value":"a:2:{i:0;a:2:{s:4:\"name\";s:6:\"Zaheer\";s:5:\"phone\";s:3:\"123\";}i:1;a:2:{s:4:\"name\";s:6:\"Sageer\";s:5:\"phone\";s:11:\"09190219218\";}}"},{"meta_id":"323","post_id":"121","meta_key":"repeatable_fields","meta_value":"a:2:{i:0;a:2:{s:9:\"iif_label\";s:4:\"City\";s:11:\"iif_details\";s:7:\"karachi\";}i:1;a:2:{s:9:\"iif_label\";s:3:\"Age\";s:11:\"iif_details\";s:2:\"12\";}}"},{"meta_id":"329","post_id":"126","meta_key":"repeatable_fields","meta_value":"a:1:{i:0;a:2:{s:9:\"iif_label\";s:3:\"Age\";s:11:\"iif_details\";s:2:\"12\";}}"},{"meta_id":"332","post_id":"128","meta_key":"repeatable_fields","meta_value":"a:3:{i:0;a:2:{s:9:\"iif_label\";s:7:\"Country\";s:11:\"iif_details\";s:8:\"Pakistan\";}i:1;a:2:{s:9:\"iif_label\";s:4:\"City\";s:11:\"iif_details\";s:9:\"Islamabad\";}i:2;a:2:{s:9:\"iif_label\";s:3:\"Age\";s:11:\"iif_details\";s:2:\"12\";}}"}]
You could try something like this:
$count_age = $wpdb->get_col( $wpdb->prepare(
"
SELECT count(meta_id)
FROM {$wpdb->prefix}postmeta
WHERE meta_value LIKE '%%%s%%'
",
'Age'
));
More about get_col() here: https://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Column

wordpress exclude posts from loop if only has one specific category

I have this category which is "community-posts" I don't want it to appear on my homepage loop so I added this to my query
<?php query_posts(array('showposts' => 4,'category__not_in' => $id_communityposts,));?>
This is working fine with me but some "community-posts" I want them to be featured on the homepage loop. (exception)
so I want to only exclude the posts that has one category as "community-posts" if it has this category and more its shows normally.
First thing do not use query_posts - it should never be used as it alter the main query. Use get_posts instead - it's much safer and perform the same task.
To answer your question, let's first imagine how the query would look in SQL (assuming your $id_communityposts is equal to 2) :
SELECT DISTINCT wp_posts.*
FROM wp_posts, wp_postmeta
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)
WHERE
wp_posts.ID = wp_postmeta.post_id AND
(
(wp_term_taxonomy.taxonomy = 'category' AND wp_term_taxonomy.term_id NOT IN(2))
OR
(wp_postmeta.meta_key = 'featured' AND wp_postmeta.meta_value = 1)
)
ORDER BY wp_posts.post_date DESC
LIMIT 4
So we query the post, post meta and taxonomy tables and make two possible conditions:
The category ID is not 2, OR
The featured meta key of the post is set to 1 (change this to whatever key / value depending of how you store the "featured" information).
For that kind of specific cases, get_posts isn't really good to play with - querying the DB with WPDB will give you much more flexibility.
$posts = $wpdb->get_results(
"SELECT DISTINCT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
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->posts.ID = $wpdb->postmeta.post_id AND
(
($wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id NOT IN(2))
OR
($wpdb->postmeta.meta_key = 'featured' AND $wpdb->postmeta.meta_value = 1)
)
ORDER BY $wpdb->posts.post_date DESC
LIMIT 4"
);
Let me know if you run into any issue as it is an untested query.
If I understood the question correctly , The simplest solution, not involving complicated SQL would be something along the lines of :
// NOT TESTED !
if ( count(get_the_category()) > 1 ) { // this means there are more than single category
// show the desired posts
} else {
// dont show
}
read get_the_category() in codex
Along the same logic lines you could also use wp_get_post_categories

WordPress: How can add posts without category into category

I have some posts in my WordPress without category, Its above 2000 article, So i need to add this article into category that name is " News " by SQL
This should work:
UPDATE wp_term_relationships SET term_taxonomy_id = '3' WHERE term_taxonomy_id = '1';
The 3 is the id of the category "News", you might need to change it if it differs for you. The 1 is the category id for "Uncategorised", so the above should change all the post ids of 1 (Uncategorised) to 3 (News)
EDIT try:
UPDATE wp_term_relationships SET term_taxonomy_id = '3' WHERE term_taxonomy_id = '';
I didn't find the answer to this question, so maybe it will be helpful for someone.
you can try this code, but instead 777 - add your category id
INSERT IGNORE INTO wp_term_relationships
(object_id, term_taxonomy_id, term_order)
SELECT
wp_posts.ID as object_id,
777 as term_taxonomy_id,
0 as term_order
FROM wp_posts
LEFT JOIN
wp_term_relationships rel
ON wp_posts.ID = rel.object_id
LEFT JOIN
wp_term_taxonomy tax
ON tax.term_taxonomy_id = rel.term_taxonomy_id
AND tax.taxonomy = 'category'
LEFT JOIN
wp_terms term
ON term.term_id = tax.term_id
WHERE wp_posts.post_type = 'post'
AND wp_posts.post_status = 'publish'
AND term.term_id is null
UPDATE FROM `wp_posts` WHERE `post_category` SET `post_category` = '9' WHERE `post_category` = '';

How to query WordPress Posts order by rating based on another table that contents the ratings for each post ID

I have a table that saves the ratings for each post, the table looks like this http://d.pr/YIxL it saves the ID of the post and the rating, I need to query the posts based on that table ASC or DSC.
I hope that the answer is clear.
This was the query that did the trick
$wp_query->request = "
SELECT $wpdb->posts.* FROM $wpdb->posts
Left join $ratings_table
ON $wpdb->posts.ID = $ratings_table.ID
Where
$wpdb->posts.post_type = 'games' AND $wpdb->posts.post_status = 'publish'
ORDER BY $ratings_table.points DESC
LIMIT $ppp OFFSET $offset";
$pageposts = $wpdb->get_results($wp_query->request, OBJECT);

Resources