$wpdb query not running - php

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.

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);

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!

Related

WP post__not_in another query not filter

my first query is okay
$ids = [];
$novidades = get_posts( array(
'posts_per_page' => 4,
'meta_key' => 'meta-checkbox',
'meta_value' => 'yes'
) );
if ( count( $novidades ) ) {
foreach( $novidades as $novidade ) {
$ids[] = $novidade->ID;
}
}
//rest of my code is ok
but, i try post another post and ignore the first query, but don't work, list all post
$args2 = array(
'post_type' => 'post',
'posts__not_in' => $ids
);
$featured = new WP_Query($args2);
Can help me?
It's post__not_in. Remove the extra s from your code.
post__not_in (array) - use post ids. Specify post NOT to retrieve. If this is used in the same query as post__in, it will be ignored.
Your code should be:
$args2 = array(
'post_type' => 'post',
'post__not_in' => $ids,//<====extra 's' removed
);
$featured = new WP_Query($args2);

WP->Query get Custom Post ID from meta_value & meta_key

I have been trying for a couple of hours to make this work - but for some reason its simply to difficult for me. I have a custom post_type 'house', and I want to find the post_id of my custom post_type with a meta_key and certain meta value.
Lets say i want to find post_id for a house with
meta_key='house_id'
meta_value='231sd1223'
How exactly would i do that with wp->query?
Here you have the query even with a loop. However, querying meta values is making more DB queries, consider looping throught "house" post type and than doing something only if meta_value is equal the house number.
// WP_Query arguments
$args = array (
'post_type' => array( 'house' ),
'post_status' => array( 'publish' ),
'meta_query' => array(
array(
'key' => 'house_id',
'value' => '231sd1223',
),
),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
global $wpdb;
$results = $wpdb->get_results( "select post_id, meta_key from $wpdb->postmeta where meta_value = '231sd1223.'", ARRAY_A );

Bulk wpdb insert for all posts [Wordpress]

I've managed to save a string into Wordpress database. The problem is that I am only able to insert the string when this code runs in a single url. I've got around 1500 posts in my database so I'd like to make it work for all of them.
global $wpdb;
$result = $wpdb->replace(
$wpdb->postmeta,
array(
'post_id' => $post->ID,
'meta_key' => 'ratingpost',
'meta_value' => $ratingvalue
),
array(
'%s',
'%s',
'%s'
)
);
I'd like to make this insert in all the posts without having to load every single url. I suppose I should do that via functions.phpbut I haven't found a proper way to do it.
Yes, you can achieve this by putting this code into functions.php , but this will insert all post every time site loads, so add this once and remove after complete execution.
<?php
global $wpdb;
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
$ratingvalue = get_post_meta($post->ID,'ratingpost',true);
$result = $wpdb->replace(
$wpdb->postmeta,
array(
'post_id' => $post->ID,
'meta_key' => 'ratingpost',
'meta_value' => $ratingvalue
),
array(
'%s',
'%s',
'%s'
)
);
<?php endforeach;
wp_reset_postdata();?>
Just use SQL for this:
global $wpdb;
$updated = $wpdb->query( UPDATE {$wpdb->postmeta} pm, {$wpdb->posts} p SET pm.meta_value = {$ratingvalue} WHERE pm.meta_key = 'ratingpost' AND p.ID = pm.post_id );

Wordpress - Meta query with post id

Can I add ID to the $args array? I need to check if the key value pair custom field exists for a particular post. Now it checks if the key value pair exists in any post. Or do I need to perform the query and then check for my value in the returned array?
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'ID' => $_POST['post_id'],
'meta_query' => array(
array(
'key' => 'claim',
'value' => $user_ID
)
)
);
// perform the query
$query = new WP_Query( $args );
$vid_ids = $query->posts;
if ( empty( $vid_ids ) ) {
add_post_meta( $_POST['post_id'], 'claim', $user_ID );
}else{
echo "sorry";
}
Please reference the Codex entry for post/page parameters for WP_Query().
You can pass single post id with this
$query = new WP_Query( array( 'p' => 7 ) );
If you want to pass multiple post id's use
$myarray = array('100', '222');
$args = array(
'post_type' => 'post',
'post__in' => $myarray
);
// The Query
$the_query = new WP_Query( $args );
Use get_post_meta to get custom field value for your object.
$claims=get_post_meta($ID,'claim');
$exists=false;
if(count($claims)>0)
{
foreach($claims as $claim)
{
if($claim==$user_ID)
{
$exists=true;
break;
}
}
}
if(!$exists)
{
add_post_meta( $_POST['post_id'], 'claim', $user_ID );
}

Show latest post from each author if post is not older than a month

I have a list of the authors (Wordpress) on the site that appears on every page so the list exists outside of the loop.
I managed to show every authors' image with their names but I would like to get their latest post title that links to the post. The post title should only show when the post is not older than a month.
Any help would be appreciated.
Thanks
<?php
global $wpdb;
$query = "SELECT ID, user_nicename from $wpdb->users WHERE ID != '1' ORDER BY 'ASC' LIMIT 20";
$author_ids = $wpdb->get_results($query);
foreach($author_ids as $author) :
// Get user data
$curauth = get_userdata($author->ID);
// Get link to author page
$user_link = get_author_posts_url($curauth->ID);
$post_link = get_permalink($curauth->ID);
// Set default avatar (values = default, wavatar, identicon, monsterid)
$main_profile = get_the_author_meta('mainProfile', $curauth->ID);
$hover_profile = get_the_author_meta('hoverProfile', $curauth->ID);
$award_profile = get_the_author_meta('awardProfile', $curauth->ID);
?>
You can use WP_Query to create a new loop for you instead. It accepts a cool date_query argument since version 3.7. Untested, but should work.
EDITED:
$args = array(
'showposts' => 1,
'orderby' => 'date',
'date_query' => array(
array(
'after' => array(
'year' => date( "Y" ),
'month' => date( "m", strtotime( "-1 Months" ) ),
'day' => date( "t", strtotime( "-1 Months" ) ),
),
'inclusive' => true,
)
) );
$query = new WP_Query( $args );
Then you can just run a regular loop
// run the loop with $query
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo 'Latest post: ' . get_the_title();
}
} else {
// no posts
}

Categories