0

@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)"?

closed as off-topic by Rarst Aug 4 '16 at 17:03

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the 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
  • 1
    Could you please post your solution as an answer, and then accept it? Thanks – shea Dec 21 '12 at 20:24
  • 1
    If you have found the solution, post it as an answer, do not edit your question – Tom J Nowell Mar 4 '13 at 0:35
1

OP found the solution:

Problem solved, found spaces in the array $arrname.

I'm posting as a brief answer to tidy up our site. If anyone would like to up vote it to get the question closed then that would be great :-)

Alternatively, express your view on the quality of question.

0

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

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