I have a follower button on users profiles where users can follow other users. I have a page template that shows all posts by users the currently logged in user follows. I would like to create a shortcode [following_loop] for this instead and add the query to the functions.php. I can't get the shortcode to work.

Here is the page template that I was using. How can I get this into the shortcode?

function register_shortcodes(){
   add_shortcode('following_users', 'following_users_function');
add_action( 'init', 'register_shortcodes');
function following_users_function () {

    $currentloggedinuser = get_current_user_id();

  // Get array containing only the user_id1 values.

  $followers = $wpdb->get_col(
      "SELECT user_id1 FROM {$wpdb->prefix}followers WHERE user_id2 = %d",

  sort($followers); // or apply sorting in the query above

  // Directly echo the imploded array. 
  echo implode(', ', $followers);

  $args = array(
    'author__in'=> $followers, //use the array we got from the query
    'post_type' => 'post'
    if (have_posts()) :
      while (have_posts()) : the_post();
         $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>';
   return $return_string;

Thank you in advance :)


I can't get the shortcode to work.

So I see there's a $args in your code, so if you actually want posts that match those args, then you can create a secondary query/loop like so: (replace all code from the if (have_posts()) : to the wp_reset_query();):

global $post;

// Create a secondary query
$query = new WP_Query( $args );

// Define the variable which stores the shortcode output
$return_string = '';

// Run the custom loop and append the output to $return_string. Remember, no echo!
if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : $query->the_post();
        $return_string .= '<a href="'.get_permalink().'">'.get_the_title().'</a>';

    wp_reset_postdata(); // restore the $post global - don't call wp_reset_query()

And be sure to add global $wpdb; at the top in your function because currently the variable is not defined in that function.

Also, please do not echo anything in shortcode functions because for example it can invalidate REST API responses and causes the block editor to fail in saving the post! So be sure to remove that echo implode(...);.

  • 1
    You are a genius :) Thank you so much. This works perfectly. I am still learning Wordpress and your comments are always so detailed and useful. Thank you again so much, I really appreciate your assistance :)
    – RobH
    Sep 22 '21 at 9:35
  • You're welcome :)
    – Sally CJ
    Sep 22 '21 at 10:03
  • PS: I've just revised my answer - I mainly moved wp_reset_postdata(); to inside the if block, so be sure to do the same. And I also added a link to the 1st paragraph, so I suggest you to check it out to learn out more about The Loop. =)
    – Sally CJ
    Sep 25 '21 at 1:02

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.