Join users and usermeta tables - WordPress - wordpress

I want to select columns from users and usermeta tables.
This is my query:
$sql = $wpdb->prepare(
"SELECT {$wpdb->users}.ID FROM {$wpdb->users}
WHERE {$wpdb->users}.user_registered <= '%s'
AND {$wpdb->usermeta}.meta_key='custom_status'
AND {$wpdb->usermeta}.meta_value=0
INNER JOIN {$wpdb->usermeta} ON {$wpdb->users}.ID= {$wpdb->usermeta}.user_id", $before);
, but it doesn't select nothing. I know that I have one user with this meta_value and user_registered less than the current date time ($before).
This is what $before is like: 2019-07-06 19:03:55

The problem is that you're wrapping the %s placeholder in single quotes. That's not needed because WordPress will replace all instances of %s with a proper string containing the value you passed to the prepare() method.
So, taking that into consideration, your code becomes:
$sql = $wpdb->prepare(
"SELECT {$wpdb->users}.ID FROM {$wpdb->users}
WHERE {$wpdb->users}.user_registered <= %s
AND {$wpdb->usermeta}.meta_key='custom_status'
AND {$wpdb->usermeta}.meta_value=0
INNER JOIN {$wpdb->usermeta} ON {$wpdb->users}.ID= {$wpdb->usermeta}.user_id", $before);
Additionally, the INNER JOIN ... part of your query has to be placed before your WHERE clause or else MySQL will throw an invalid syntax error:
$sql = $wpdb->prepare(
"SELECT {$wpdb->users}.ID FROM {$wpdb->users}
INNER JOIN {$wpdb->usermeta} ON {$wpdb->users}.ID= {$wpdb->usermeta}.user_id
WHERE {$wpdb->users}.user_registered <= %s
AND {$wpdb->usermeta}.meta_key='custom_status'
AND {$wpdb->usermeta}.meta_value=0", $before);

Related

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

Fetch data from database from multiple tables matching condition

I want to fetch data from the database. I have three tables in my database: listing_master_residential, listing_master_condo and listing_master_commercial. There is one primary key, Ml_num, in all tables. I want to search the data from one table which matches mls number table.
if (isset($_POST['search'])) {
$mls=$_POST['mls_number'];
$sql = "SELECT * FROM listing_master_residential,
listing_master_condo,
listing_master_commercial
INNER JOIN listing_master_residential AS res ON res.Ml_num=Ml_num
INNER JOIN listing_master_condo AS con ON con.Ml_num=Ml_num
INNER JOIN listing_master_commercial AS com ON com.Ml_num=Ml_num
WHERE Ml_num='$mls'";
$result = $wpdb->get_results($sql) or die(mysql_error());
foreach ( $result as $row) {
echo $row->Lot_code."<br/>";
echo $row->Ml_num."<br/>";
echo $row->Acres;
echo $row->Addr."<br/>";
echo $row->Bath_tot;
echo $row->Br;
echo $row->Br_plus;
}
}
With the above, I get an error:
Column 'Ml_num' in where clause is ambiguous
Because the column Ml_num is in all the tabels you need to address it with the Fully Qualified Name, for instance con.Ml_num
I did also notice your joining all the tabels twice, once with implicit join and once with ANSI join
The implicit join will result in a cross join because there are no join conditions in the where clause.

Wordpress sorting by either post meta key or post_date

I'm having a little trouble coming up with the correct SQL for a Wordpress query.
What I have are some posts that contain a custom field called 'date'. I'd like to fetch ALL posts (both those containing this custom field plus those that don't) and sort them by date AND post_date. date will take precedence over post_date, i.e., if there's not a date specified, it'll use the post_date value instead.
What I have so far:
$query = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'date'
AND wposts.post_type = 'post'
ORDER BY wpostmeta.meta_value, posts.post_date ASC
";
Thx

query 2 wordpress database tables at once

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

Why doesn't this WordPress $wpdb query work?

I am doing the following:
$type = 'attachment';
$images = $wpdb->get_results($wpdb->prepare('
SELECT p.*
FROM wp_%d_posts p
WHERE p.post_parent =%d
AND p.post_type = "%s"
', $blog_id, $page->ID, $type),OBJECT);
var_dump($images);
If I remove the line 'AND p.post_type = "%s"' then I get results returned, otherwise I get an empty array returned. If I run the query direct against the DB in a mysql client, I get results.
There is no error, just an empty result set. I am doing similar queries throughout my file and they are working so I'm not looking for "don't do it like that" style replies. I just need to understand why this isn't working and fix it.
PHP 5.3, MYSQL 5.1. WordPress MU 2.9.2
Do not Quote "%s". From the WordPress site, "Notice that you do not have to worry about quoting strings. Instead of passing the variables directly into the SQL query, use a %s placeholder for strings and a %d placedolder for integers."
Example:
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_title = %s WHERE ID = %d", $var, $id ) );

Resources