0

I'm having trouble with calling an imploded variable that is then formed to an array and called in a post loop argument.

$user_id = um_profile_id(); // Get current user ID
                    $follow_ids = $wpdb->get_results( "SELECT meta_value FROM brilli_usermeta WHERE user_id = ".$user_id );
                    $array = array();
                    foreach( $follow_ids as $output ){
                        $array[] = $output->meta_value;
                    }
                    $following = implode(',', $array);
                    $return = array(print_r($following));
                    $args = array(
                        'author__in' => $return,
                    );
                    query_posts( $args );
                    while ( have_posts() ) : the_post();
                        echo '<p>' . get_the_title() . '</p>';
                    endwhile;
                    wp_reset_query();

This is what it is outputting:

enter image description here

This doesn't display the correct posts, and it displays the IDs of the person they're following.

I need it to return the posts by the IDs that are stored in the array.

3
  • Could you be a little more specific? :) What trouble are you having? What problem is happening and at what point does it occur?
    – WebElaine
    May 8 '18 at 17:26
  • See my updated question. May 8 '18 at 17:50
  • Why do you query for user meta via a raw SQL query? That query will pull in all user meta from all plugins and core itself, not just yours
    – Tom J Nowell
    May 8 '18 at 19:18
0

This is your problem:

$follow_ids = $wpdb->get_results( "SELECT meta_value FROM brilli_usermeta WHERE user_id = ".$user_id );

No meta key is specified, and it's a raw SQL query, which means all user meta values are returned, even those that aren't relevant. You need to specify a key

But why bother with a direct query, when a cached API is available, thats much easier to use, much safer, and much faster?

$follow_ids = get_user_meta( $user_id, 'follow_ids', false );
foreach( $follow_ids as $id ) {
    // etc ...
}

Where 'follow_ids' is to be replaced with the meta key you used to store your followers IDs in

Additional notes:

  • You should be using taxonomy terms or this not user meta, especially if you ever want to show a list of users who follow a person
  • Never use query_posts, there is no valid or good reason to use it. Use pre_get_posts to modify or replace the main query, or use WP_Query to add new queries
  • Never use raw SQL to fetch data from the DB stored in WP tables. There are functions for doing that, and they're much faster than any SQL you can write. And safer too, your query had no escaping or preparation applied
4
  • Could you be a little bit more specific on the last point. Fetching data from WP tables. May 23 '18 at 22:06
  • Don't write raw SQL unless you're calling custom tables. The tables that come with WP have APIs and functions, use those to grab data, don't use raw DB queries. You might think you're being 'faster' but you're not, you're bypassing all the caching mechanisms, and introducing complexity
    – Tom J Nowell
    May 23 '18 at 22:12
  • e.g. don't write a query to fetch a post, use get_post, don't write a query to fetch post meta, use get_post_meta etc
    – Tom J Nowell
    May 23 '18 at 22:13
  • There is a manually created table called brilli_usermeta, so it's not actually calling ALL meta values - please see my updated question. May 23 '18 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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