1

I have a loop for bookmarked/favorite posts and for a fresh account if you don't have a bookmark post on bookmark page will show this message: Bummer, no bookmarks posts!

After the fresh account add 1 post to bookmark, the post appear on bookmark page, everything good.

But if the fresh account remove that only one bookmarked post from his bookmark posts then on page will show all posts from wordpress instead of this message : Bummer, no bookmarks posts!

What I need to set to display same message and not all posts from wordpress?

Here it's my loop:

<?php
                if(!is_user_logged_in() ) {
                    ?>
                    <div class="side-article">
                        <h2><?php _e('Please login if you want to see bookmarked posts!', 'Aruna');?></h2>
                    </div>
                <?php }
                else {
        $current_user = wp_get_current_user();
        $bookmarks = get_user_meta($current_user->ID, 'bookmarks', true);
        if($bookmarks) { 
        $bookmarks = unserialize($bookmarks);
        //Bookmarks                          
        $args = array();
        $args['post_type'] = 'post';
        $args['post__in'] = $bookmarks;
        $args['order'] = 'DESC';
        $args['posts_per_page'] = 8;
        //performance improvements
        $args['no_found_rows'] = true;
        $args['update_post_term_cache'] = true;
        $args['update_post_meta_cache'] = true;

        $paged = is_front_page() ? get_query_var( 'page' ) : get_query_var( 'paged' );
        $args['paged'] = $paged;
        $wp_query = new WP_Query($args);
        if ($wp_query->have_posts()) :
        while ($wp_query->have_posts()) : $wp_query->the_post();
        get_template_part( 'content', get_post_format() );
        endwhile; wp_reset_postdata();  
        get_template_part('includes/pagination');   
        endif;
        ?>
        <?php
        } else { ?>
        <div>
        <h2><?php _e("Bummer, no bookmarks posts!", 'Aruna');?></h2>
        </div>
       <?php }
       } ?>

Functions.php:

function teo_bookmark_submit() {
    // get the submitted parameters
    $postID = $_POST['post_id'];

    $current_user = wp_get_current_user();
    $bookmarks = get_user_meta($current_user->ID, 'bookmarks', true);
    if(empty($bookmarks) ) {
        $bookmarks = array();
    }
    else {
        $bookmarks = unserialize($bookmarks);
    }
    if(!in_array($postID, $bookmarks) ) {
        $bookmarks[] = $postID;
    }
    $bookmarks = serialize($bookmarks);
    update_user_meta($current_user->ID, 'bookmarks', $bookmarks);
    exit;
}

function teo_bookmark_remove() {
    // get the submitted parameters
    $postID = $_POST['post_id'];

    $current_user = wp_get_current_user();
    $bookmarks = get_user_meta($current_user->ID, 'bookmarks', true);
    $bookmarks = unserialize($bookmarks);

    if(in_array($postID, $bookmarks) ) {
        $index = array_search($postID, $bookmarks);
        unset($bookmarks[$index]);
    }

    $bookmarks = serialize($bookmarks);
    update_user_meta($current_user->ID, 'bookmarks', $bookmarks);
    exit;
}
3
  • Since the key conditional depends on $bookmarks, it would be helpful to see how the user_meta is being updated to produce $bookmarks = false, and serialized.
    – CK MacLeod
    Commented Jul 1, 2016 at 15:32
  • Hey CK MacLeod, thanks for helping me. I updated the post
    – I.Alex
    Commented Jul 1, 2016 at 15:40
  • Haven't really helped you yet, but I'll say as a general comment before looking at things in detail that I suspect the problem may be in all of the serializing/unserializing.
    – CK MacLeod
    Commented Jul 1, 2016 at 15:56

1 Answer 1

0

OK - so, first, you don't need to serialize/unserialize. get_user_meta() will return an array, and delete_user_meta() can remove a specific value.

So, simplifying a few other things here and below, your remove function would work like this (very simple):

function teo_bookmark_remove() {

    $postID = $_POST['post_id'];
    $current_user = get_current_user_id();

    delete_user_meta( $current_user, 'bookmarks', $postID );

}

Likewise bookmarks submit: Very simple (though probably here and in the previous function you should be using nonces or other security features- not sure about how rest of the set-up is handling the matter):

function teo_bookmark_submit() {

    // get the submitted parameters
    $postID = $_POST['post_id'];
    $current_user = get_current_user_id();

    update_user_meta( $current_user, 'bookmarks', $postID );

}

Now the display function:

if (!is_user_logged_in() ) {

    ?>
    <div class="side-article">
        <h2><?php _e('Please login if you want to see bookmarked posts!', 'Aruna');?></h2>
    </div>

    <?php } else {

    $current_user = get_current_user_id();
    $bookmarks = get_user_meta($current_user, 'bookmarks'); //returns array

    if ( ! empty($bookmarks) ) { 

            $args = array();
            $args['post_type'] = 'post';
            $args['post__in'] = $bookmarks; //post__in takes array
            $args['order'] = 'DESC';
            $args['posts_per_page'] = 8;
            //performance improvements
            $args['no_found_rows'] = true;
            $args['update_post_term_cache'] = true;
            $args['update_post_meta_cache'] = true;

    $paged = is_front_page() ? get_query_var( 'page' ) : get_query_var( 'paged' );
    $args['paged'] = $paged;

    $wp_query = new WP_Query($args);

    if ($wp_query->have_posts()) {
        while ($wp_query->have_posts()) : $wp_query->the_post();

            get_template_part( 'content', get_post_format() );

        endwhile; 

        wp_reset_postdata();  

        get_template_part('includes/pagination');   

    }

} else { ?>

    <div>
        <h2><?php _e("Bummer, no bookmarks posts!", 'Aruna');?></h2>
    </div>

   <?php }

} 

Tested the "no bookmarks" part - the part that was giving you problems. Can't vouch completely for the results of the $bookmarks not empty alternative, of course, but works with a simple is_empty test.

Note: I believe on the above functions, the same value may end up appearing multiple times in the 'bookmarks' array. I don't think it's a problem for post__in or delete_post_meta (will delete all instances of particular value associated with the key), but may be if you use the 'bookmarks' array for other purposes. Not hard hard to fix that if you need to.

13
  • Hey CK MacLeod , again thanks for trying to help me but functions.php codes broke my bookmark script. I mean, when i bookmark a post it doesn't save bookmark page doesn't show nothing with that code, message no , bookmarks no because it doesn't save in user meta , I guess.. Basically without serialize and unserialize it doesn't save bookmarks posts
    – I.Alex
    Commented Jul 1, 2016 at 18:42
  • Not sure I understand exactly, and not in a position to debug the script... we'd also have to see the input code, I think, for starters. Standard procedure on the $_POST variables would be to start with an "isset," then go through a nonce, then do an if ($_POST === ...) etc. Probably should have noted in the simplified versions...
    – CK MacLeod
    Commented Jul 1, 2016 at 18:52
  • Key point, I think, is that serialize and unserialize shouldn't be necessary at any point. The functions already handle that.
    – CK MacLeod
    Commented Jul 1, 2016 at 18:54
  • With what you give me for functions.php doesn't work. I bookmark a post : imgur.com/zIPX4Th then F5 to refresh the page and the bookmark post doesn't save imgur.com/MyYUgTf . I don't think will work without serialize and unserialize and on bookmark page if I delete this : $bookmarks = unserialize($bookmarks); basically will broke my page imgur.com/3v7EDB8
    – I.Alex
    Commented Jul 1, 2016 at 18:59
  • I don't understand - when you say you're bookmarking the post, what function/input submit the post ID as a $_POST variable?
    – CK MacLeod
    Commented Jul 1, 2016 at 19:04

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.