Categories

Friday, November 22, 2024
#919814419350 therichposts@gmail.com
Wordpress Hooks

How to get logged in user recently read posts in wordpress?

How to add custom meta title and meta description in Wordpress?

Get logged in user recently read posts in wordpress

Hello, welcome to therichpost.com. In this post, I will tell you, How to get logged in user recently read posts in wordpress?  WordPress is the best cms. WordPress hooks(add_action, add_filter) give us the power to edit or change the code without interruption into the files and this is the best thing about wordpress. Now I am going to tell you how the hooks work.

In this post, I will do the trick that, if user will logged in and he/she wants to check recent read post or article and then he/she will easily track with below code and this is very interesting.

Here is complete working and tested code to Get logged in user recently read posts in wordpress:

You need to add below code into your theme’s functions.php file:

function shortcode_update_recent() {
    if( is_user_logged_in() ) {
        $post_id = get_the_ID();
        $uu_id = get_current_user_id();

        add_post_meta($post_id,'_post_read_by', get_current_user_id(), false);
    }
}
add_shortcode('track_user_recently_read', shortcode_update_recent );

function shortcode_recent() {
     $uu_id = get_current_user_id();

    $args = array(
        'posts_per_page'   => 10,
        'meta_key'         => '_post_read_by',
        'meta_value'       => $uu_id,
        'post_type'        => 'post',
        'post_status'      => 'publish',
    );
  

    $posts_array = get_posts( $args );
    foreach ( $posts_array as $post ) : 
     echo $post->post_title;
    endforeach; 
    wp_reset_postdata();
}
add_shortcode('display_user_recent', shortcode_recent );

 You need to add below code into your theme’s single.php file:

echo do_shortcode('[track_user_recently_read]');

 You need to add below code into your theme’s template file, where you want show logged-in user recent read posts  and I am just getting the post title but we can get all the post content like post feature image and many more :

<?php echo do_shortcode('[display_user_recent]'); ?>

 Now you are done and if you have query related to this post or you want to do some more with this code then please do comment below and I will come with wordpress hooks.

 

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.