Reputation: 2615
I'm working on a wpdb query and it's just not running. And for some reason the error reporting for wpdb isn't giving me any errors.
So, can anyone spot an obvious mistake in this query?
I'm attempting to get the meta_value
of all _sell_media_attached_file
keys in the post_meta
table.
I'm first running a wp_query
, getting the post ID's and then running each post ID through the wpdb query.
Here's what I'm working with:
// run the loop
$loop = new WP_Query( array(
'post_type' => 'sell_media_item',
'collection' => $club,
'include_children' => false,
'year' => $year,
'monthnum' => $month,
'day' => $day,
'fields' => 'ids',
) );
if ( $post_ids = $loop->get_posts() ) {
$post_ids = implode( ',', $post_ids );
//breaks here
$atts_ids = $wpdb->get_col( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id = $post_ids AND meta_key = '_sell_media_attached_file' " );
echo('<b>query:</b> <pre>'); var_dump($atts_ids);
But as I say, nothing get's output in the var_dump
and if I add $wpdb->print_error();
I get no errors.
Upvotes: 1
Views: 517
Reputation: 64486
In your piece of code i didn't see the initialization of wpdb
class to access the wpdb
class in your wordpress you have to define it as the global
to access the functions of the class and also if $post_ids
contains the multiple ids with comma separated you have to use the IN()
clause instead of =
operator
global $wpdb;
// run the loop
$loop = new WP_Query( array(
'post_type' => 'sell_media_item',
'collection' => $club,
'include_children' => false,
'year' => $year,
'monthnum' => $month,
'day' => $day,
'fields' => 'ids',
'nopaging'=>true
) );
if ( $post_ids = $loop->get_posts() ) {
$post_ids = implode( ',', $post_ids );
//breaks here
$atts_ids = $wpdb->get_col( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id IN($post_ids) AND meta_key = '_sell_media_attached_file' " );
echo('<b>query:</b> <pre>'); var_dump($atts_ids);
Upvotes: 3
Reputation: 1566
why not try something like that?
if( $loop->have_posts() ){
$ids = $loop->get_posts();
foreach( $ids as $id ){
$atts_ids[] = get_post_meta($id, '_sell_media_attached_file', true|false ); //true if single value false if multiple values return as array
}
}else{
//Do something else
}
Hope it helps!
Upvotes: 0