I have this custom query I need to do that will check against both post_metadata values and taxonomy terms. I'm using code that I modified from Wordpress's codex, but it is returning zero results consistently. Is there a mistake I've made? (UPDATED: shows where it is being loaded into a variable first)
global $wpdb;
$querystr = "
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
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)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->terms.name = '" . $service . "'
AND $wpdb->term_taxonomy.taxonomy = 'Services'
AND $wpdb->postmeta.meta_value = '" . $county . "'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->postmeta.meta_key = 'order'
ORDER BY $wpdb->postmeta.meta_value ASC";
$pageposts = $wpdb->get_results($querystr, OBJECT);
I would suggest logging the actual string that is executed (save this select to a variable and log the variable, then pass the variable to the wpdb query). Once you have the sql, you can more easily troubleshoot it (or post it here for further assistance).
Related
I'm trying to show the total value for all orders in certain status whose payment method is "ppec-paypal".
I've tried using the AND function to filter by meta.meta_value
add_shortcode( 'show_total_pp', 'show_total_pp_shortcode' );
function show_total_pp_shortcode() {
global $wpdb;
$order_totalpp = apply_filters( 'woocommerce_reports_sales_overview_order_totals', $wpdb->get_row( "
SELECT SUM(meta.meta_value) AS total_sales, COUNT(posts.ID) AS total_orders FROM {$wpdb->posts} AS posts
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
WHERE meta.meta_key = '_order_total'
AND posts.post_type = 'shop_order'
AND meta.meta_value = 'ppec_paypal'
AND posts.post_status IN ( '" . implode( "','", array( 'wc-processing','wc-on-hold' ) ) . "' )
" ) );
return absint( $order_totalpp->total_sales);
echo $order_totalpp;
}
I have this code string working for orders based on just the status's, but when I try to filter the payment method by adding the line:
" AND meta.meta_value _payment_method = 'ppec_paypal' "
it does not work, gets a '0' value.
I'm guessing there is syntax error in my code.
for a left join you should not use column related to the left joined table in where clause (this way work as an inner join)
but you should add this condition to the related ON clause
SELECT SUM(meta.meta_value) AS total_sales, COUNT(posts.ID) AS total_orders
FROM {$wpdb->posts} AS posts
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
AND meta.meta_key = '_order_total'
AND meta.meta_value = 'ppec_paypal'
WHERE posts.post_type = 'shop_order'
AND posts.post_status IN ( '" . implode( "','", array( 'wc-processing','wc-on-hold' ) ) . "' )
I try to get the post_ids from postmeta where the _sale_price_dates_to are less the now. And I did something like this:
global $wpdb;
$publishid = $wpdb->get_results('SELECT meta_value FROM ' . $wpdb->postmeta . ' WHERE meta_key="_sale_price_dates_to" and meta_value<=' .time().'') or die("Error: Cannot create object");
But it doesn't work. Maybe because the _sale_price_dates_to has format longtext not date. how to change it in the query?
But It's not over. I want to add the condition that the products have to be published then I have to join:
$publishid = $wpdb->get_results('SELECT meta_value FROM ' . $wpdb->postmeta . 'join' . $wpdb->posts . ' ON wp_posts.ID = wp_postmeta.post_id WHERE wp_postmeta.meta_key="_sale_price_dates_to" and wp_postmeta.meta_value<=' .time().' and wp_posts.post_status="publish" ') or die("Error: Cannot create object");
But because of the first problem, I don't know it works.
Update: Setting up the right time zone in the code
Try the following to make a functional SQL query using WPDB to get the Post IDs where post meta _sale_price_dates_to timestamp is less the now time stamp:
global $wpdb;
// Set the correct time zone (http://php.net/manual/en/timezones.php)
date_default_timezone_set('Europe/Paris');
// An array of IDs
$results = $wpdb->get_col("
SELECT p.ID
FROM {$wpdb->prefix}posts AS p
JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
WHERE p.post_status = 'publish'
AND pm.meta_key = '_sale_price_dates_to'
AND pm.meta_value <= '".time()."'
AND pm.meta_value != ''
");
// Raw Output (array of post IDs)
print_r($results);
Tested and works.
meta_value != '' is needed to avoid Post IDs with empty values to be queried.
I am working on pagination in WordPress, pagination working fine for all posts, But I need posts based on category
Now I am using this query for getting all post
$per_page = 8;
$start = $page * $per_page;
$page = sanitize_text_field($_POST['page']);
$wpdb->get_results($wpdb->prepare("
SELECT * FROM " . $table_name . " WHERE post_type = 'articles' AND post_status = 'publish' ORDER BY post_date DESC LIMIT %d, %d", $start, $per_page ));
Can anyone tell me how to display post based on category, I am using custom post type('articles').
Thanks
I have tried with this that is working with me.You may help this code.
Here, you have to join some tables to get post based on category. For that I used join in query.You are using ajax and you will get category_name in POST and that will be assign to related table.
Updated:
$per_page = 8;
$start = $page * $per_page;
$page = sanitize_text_field($_POST['page']);
$category_name = $_POST['category_name'];
$wpdb->get_results($wpdb->prepare("SELECT * 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)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->terms.name = ''
AND $wpdb->term_taxonomy.taxonomy LIKE '%".$category_name."%'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'articles'
ORDER BY post_date DESC LIMIT %d, %d", $start, $per_page));
EDIT: I elaborated more on this topic in another thread: List categories by author ~ WITH COUNTER ~ (Wordpress) ... it might be more suitable for you.
When I'm in my Category page I want the readers to see who's posting there, and filter by Author. (Ex. Activism has 127 post, 37 by Adrian, 12 by James, etc…) I've tried a million things out there, but nothing serves my simple task! Anyone with similar experience out there?
This is what I've got:
<?php
$curauth = $wp_query->get_queried_object();
$auth_id = 1;
$cat_id = 2411;
$sql = "SELECT COUNT(*)
FROM $wpdb->posts p
JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id)
WHERE post_author = $auth_id
AND post_type = 'post'
AND post_status = 'publish'
AND post_date <= NOW()
AND tr.term_taxonomy_id = $cat_id
";
$post_count = $wpdb->get_var($sql);
echo "This Author has published $post_count posts in Activism<br />";
?>
EDITED:
OK, this code works!!! ... I'm happy, but still a bit puzzled. Why does this work, and the pne above not?
<?php
$auth_id = 1;
$cat_id = 2411;
$counter = "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 = $cat_id
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND post_author = '$auth_id'
";
$user_count = $wpdb->get_var($counter);
echo "Author... has $user_count posts in Activism<br />";
?>
I am running a competition which is based on WP Custom Fields. I am trying to count all meta values of a meta_key='odd' for a specific user. I am using the formula below but it's not returning any result.
$meta_key = 'odd';
$post_author = 'Admin';
$odd = $wpdb->get_var( $wpdb->prepare(
"
SELECT Count(meta_value)
FROM $wpdb->postmeta
INNER JOIN $wpdb->posts ON $wpdb->postmeta->post_id = $wpdb->posts->ID
WHERE $wpdb->postmeta->meta_key = 'odd' AND $wpdb->posts->post_author = 'Admin'
",
$meta_key, $post_author
) );
echo "<p>Total odd {$odd}</p>";
Anyone can help me? Thanks in advance.
You are mixing up the $wpdb variables and the $post variables - for your JOIN, for example, you can't use $wpdb->postmeta->post_id, it would be {$wpdb->postmeta}.post_id, and would be even better to just use table aliases. Your prepare() is also not passing in your meta_key or author because you don't have %s placeholders for them
$odd = $wpdb->get_var( $wpdb->prepare(
"
SELECT count(meta_value)
FROM {$wpdb->postmeta} pm
INNER JOIN {$wpdb->posts} p ON pm.post_id = p.ID
WHERE pm.meta_key = %s AND p.post_author = %s
",
$meta_key, $post_author
) );