How to querry Favorite Posts for non logged in users in Bricks Builder?

Support MB Favorite Posts How to querry Favorite Posts for non logged in users in Bricks Builder?Resolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #44065
    PhilippPhilipp
    Participant

    Hello,

    I am trying to querry favorite pots for non logged in users in Bricks builder using the Bricks PHP Query Editor.

    I followed the MB Favorite Post Documentation and inserted the mentioned PHP Querry. I changed the post type to my post type (which is called foerderfinder)

    <div>
    $post_ids = get_user_meta( $user_id, 'mbfp_posts', true );
    $query = new WP_Query( [
    'post_type' => 'foerderfinder',
    'post__in' => $post_ids,
    ] );
    // Do something with $query.
    </div>

    The problem: It still shows me my blog articles, hence the querry code is not working.

    I dont know where to go from here and hope someone can help me please.

    Thanks,
    Philipp

    #44070
    PeterPeter
    Moderator

    Hello,

    For logged-in users, you need to pass the user ID to this code

    $post_ids = get_user_meta( $user_id, 'mbfp_posts', true );
    

    to get the user meta. For example:

    $post_ids = get_user_meta( 123, 'mbfp_posts', true ); //123 is the user ID
    

    You can contact Bricks support and ask them how to get the user ID in the loop.
    If you are not able to complete the task, we offer a customization service with an extra fee. Please contact us here for more details https://metabox.io/contact/

    #44083
    SridharSridhar
    Participant

    Hi Peter,

    What should be the user ID to pass as the 1st argument for visitors who are not logged in?

    get_current_user_id() works for logged-in users.

    #44084
    PeterPeter
    Moderator

    Hello Sridhar,

    I just understand the user ID is the author of the post in the loop. For non-logged-in users, you can get the posts from the cookie. Please take a look at the file /wp-content/plugins/mb-favorite-posts/src/Helper.php

    For example:

    $favorites = MetaBox\FavoritePosts\Helper::get_favorites();
    foreach( $favorites as $post_id ) {
     echo $post_id;
    }

    It is noted in the documentation https://docs.metabox.io/extensions/mb-favorite-posts/#data

    #44093
    PhilippPhilipp
    Participant

    Thank you Sridhar and Peter, the querry works now!

    For anyone stumbling upon this thread, here is the solution: https://brickslabs.com/mb-favorite-posts-in-bricks/

    Now it would be great if there was a solution for the delete button in the custom PHP Querry.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.