How can I use shortcodes list as a 'source' for my loop?

Hi guys, I’m trying to make a “Favorite posts” functionality in my site (when user clicks “add to favorite” on a given post - this post will be marked as “favorited” and will appear in the “Favorites” list).

For this I use a “Favorites” plugin which provides you a shortcode for 'Favorite" button and for the “User_favorites” (to show list of iteams added to Favorites).

Now the problem: the list of favorites can be shown through the shortcode - but I can’t find a way to “loop through” this list: https://i.imgur.com/uZlBcgx.png

Example: I want to pick headlines of each of these post ids, and also the images, and design in a way that I want them to be.

Is there any way to make it work? What would be the better path?
Thanks in advance!

You can use a query loop and the bricks/posts/query_vars hook for this:

add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
    if ( $element_id == 'tkzgjj' && function_exists( 'get_user_favorites' ) ) {
        $favorites = get_user_favorites();
        $query_vars['post__in'] = ! empty( $favorites ) ? $favorites : [0];
    }

    return $query_vars;
}, 10, 3 );

Just replace the element id (in my case tkzgjj) with the element id of your query loop element (see screenshot).

Result

Here you can see it in action: Video uploaded to CleanShot Cloud

1 Like

Wow, thank you so much for your help, a kind stranger!
I spent 3 days looking for a solution with no luck - but now it works!
Thank you! :pray::pray::pray:

1 Like

Hi,
I try to do the same but it doesn’t work, can you help me?

  • i add the code to the functions.php.
  • add the element id to the code
add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
    if ( $element_id == 'tkzgjj' && function_exists( 'get_user_favorites' ) ) {
        $favorites = get_user_favorites();
        $query_vars['post__in'] = ! empty( $favorites ) ? $favorites : [0];
    }

    return $query_vars;
}, 10, 3 );