0

I have created a shortcode which shows the posts which were created after the user last logged in. The shortcode is working perfectly fine, but I am not sure why but I am getting 7 duplicate posts. So like for test I created a post named as "Sample Article", and when I echo out the title of the post, I get this result.

Sample Article Sample Article Sample Article Sample Article Sample Article Sample Article Sample Article Sample Article

Shortcode

function latest_posts_after_last_login( $atts ) {

    global $wpdb, $current_user;

    $atts = shortcode_atts( array( 'post_type' => ''), $atts, 'latest_posts_after_last_login' );

    $post_type = $atts['post_type'];

    $last_login = get_user_meta( $current_user->ID, '_last_login_for_posts', true );

    $querystr = "
        SELECT $wpdb->posts.* 
        FROM $wpdb->posts, $wpdb->postmeta
        WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
        AND $wpdb->posts.post_status = 'publish' 
        AND $wpdb->posts.post_type = '%s' 
        AND $wpdb->posts.post_date > '%s'
        ORDER BY $wpdb->posts.post_date DESC
    ";

    $prepare = $wpdb->prepare($querystr, array($post_type , $last_login));

    $pageposts = $wpdb->get_results($prepare, OBJECT);

    foreach ($pageposts as $posts) {
        echo $posts->post_title;
    }

}
add_shortcode( 'latest_posts_after_last_login', 'latest_posts_after_last_login' );

Thanks for the help in advance ...

2

Nevermind, I solved it on my own. While reading on the WordPress Codex for $wpdb, I saw that if I use OBJECT_K it will remove the duplicates and that solved my problem.

If it helps anyone, here is my working code:

function latest_posts_after_last_login( $atts ) {

    global $wpdb, $current_user;

    $atts = shortcode_atts( array( 'post_type' => ''), $atts, 'latest_posts_after_last_login' );

    $post_type = $atts['post_type'];



    $last_login = get_user_meta( $current_user->ID, '_last_login_for_posts', true );

    $querystr = "
        SELECT $wpdb->posts.* 
        FROM $wpdb->posts, $wpdb->postmeta
        WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
        AND $wpdb->posts.post_status = 'publish' 
        AND $wpdb->posts.post_type = '%s' 
        AND $wpdb->posts.post_date > '%s'
        ORDER BY $wpdb->posts.post_date DESC
    ";

    $prepare = $wpdb->prepare($querystr, array($post_type , $last_login));

    $pageposts = $wpdb->get_results($prepare, OBJECT_K);


    foreach ($pageposts as $post) {
        echo $post->post_title;
    }

}
add_shortcode( 'latest_posts_after_last_login', 'latest_posts_after_last_login' );
| improve this answer | |
  • Hi - great that you posted your answer. If you could accept it too then that will help us keep the site tidy. – Andy Macaulay-Brook Jul 22 '16 at 17:25
  • Done. Sorry for the Delay! – Fahad Sohail Aug 3 '16 at 22:28

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.