0

Wordpress database, bit stuck on this one.

I'm using the following to get the ID of the current user.

 $user_ID = get_current_user_id();

This returns something like this :

 15

Now I try to find the matching value of $user_ID in the field show_user_list The data in this field is stored in an array.

Which looks something like this :

 a:2:{i:0;s:2:"29";i:1;s:2:"15";}

This is the query i'm running (along with a set of conditions) :

global $wpdb; $result = $wpdb->get_results( "SELECT post_id FROM wp_postmeta WHERE show_user_list IN (' . implode(',', $user_ID) . ' AND post_type = 'show' AND post_status = 'publish'" );

And then I'm trying to echo the value of the matching post_id with this :

 foreach ( $result as $unique ) {
 echo $unique->post_id;
 }

But it's not returning anything. I know I must be making a mistake while dealing with the array but I don't know where I'm going wrong?

1 Answer 1

0

looks like you have stored a serialized array in the show_user_list field, so it will be a hustle to search for values into using a db query.

In the model you described, you have to select all the rows from wp_postmeta that match "post_type = 'show' AND post_status = 'publish'", then manually filter results that do not have the user id in the unserialized show_user_list field.

You might try something like : in_array($user_ID, unserialize($row->show_user_list))

Also, I noticed multiple errors in your query: string not properly concatenated with PHP code and the right parenthesis of the IN clause not closed.

Regards, same

EDIT

Here is how I would solve your problem providing info you have given :

$user_ID = get_current_user_id();
global $wpdb;
$results = $wpdb->get_results("SELECT post_id, show_user_list FROM wp_postmeta WHERE post_type = 'show' AND post_status = 'publish'");
$user_post_ids = array();
foreach ($results as $post) {
    if (in_array($user_ID, unserialize($post->show_user_list))) {
        $user_post_ids[] = $post->post_id;
    }
}

Hope this helps !

1
  • Thanks for your help Same. I really have no idea how to implement what you have suggested though. Any pointers you could offer would be great.
    – Grant
    Jan 24, 2015 at 23:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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