Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

@Milo Problem solved, found spaces in the array $arrname.

I was trying to get post id using post_name. First of all I stored all post_name in an array an then loop through the array to use the key variable in a wpdb query to get post id, really strange though, it only returns the first post id.

As you can see I tried to output the array and it works fine, outputs every key value in that array,but the wpdb query only return the first result(tried switch the order of the key values in the array, confirmed that it only output the first result.).

Any ideas?

global $wpdb;
$rawname=$_POST['image_post_name'];
$arrname=explode(",", $rawname);
foreach ($arrname as &$value) {
$mypostid = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_name='$value'"));
echo $mypostid;
echo $value;}

Tried to add exit( var_dump( $wpdb->last_query ) );, found out only the first query got executed.

string(49) "SELECT ID FROM wp_posts WHERE post_name='unamed3'" last query, "String(49)"?

share|improve this question
    
Found this post, similar problem. wordpress.stackexchange.com/questions/69401/… –  lancerq Oct 27 '12 at 21:11
    
Tried to add exit( var_dump( $wpdb->last_query ) );, found out only the first query got executed. –  lancerq Oct 27 '12 at 21:16
    
string(49) "SELECT ID FROM wp_posts WHERE post_name='unamed3'" last query, "String(49)"? –  lancerq Oct 27 '12 at 21:21
    
wordpress.stackexchange.com/questions/27526/… another similar case. –  lancerq Oct 27 '12 at 21:26
    
does the string of values have spaces after the commas? there's nothing wrong with that code as long as your post name values are correct. –  Milo Oct 27 '12 at 22:03
show 6 more comments

1 Answer

I believe your problem is data validation - You should always use sanitation functions when dealing with Database. Try to see if it helps..

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.