• Resolved ehsanmn

    (@ehsanmn)


    I want to show least viewed posts from all categories except one,

    by this code I can show least viewed posts : source

    and with below code, I can show all posts except posts belong to specific category (form example cat with id=73)

    $query ="
    SELECT *
    FROM wp_posts
    LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
    LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
    WHERE wp_term_taxonomy.term_id NOT IN (73)
    GROUP BY wp_posts.ID limit 2";
    /---- FOR DISPLAY RESULT -----/
    $resultfirst = $wpdb->get_results($query);
    foreach( $resultfirst as $result ){
    echo $result->post_title;
    echo ' - ';
    }

    Is there a way to combine these two codes? In such a way that the posts with the least visits are displayed, provided that they are not from category 73

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hey @ehsanmn,

    Here’s how I would do it:

    function NotSoPopV2() {
    global $wpdb;

    $return = "<ul>";

    $query = "SELECT ppd.pageviews, p.*
    FROM {$wpdb->posts} p
    LEFT JOIN {$wpdb->term_relationships} pt
    ON pt.object_id = p.ID
    LEFT JOIN {$wpdb->term_taxonomy} t
    ON t.term_taxonomy_id = pt.term_taxonomy_id
    RIGHT JOIN {$wpdb->prefix}popularpostsdata ppd
    ON ppd.postid = p.ID
    WHERE p.post_type = 'post' AND p.post_status = 'publish' AND t.taxonomy = 'category' AND t.term_id NOT IN(73) ORDER BY ppd.pageviews ASC limit 10;";

    $tops = $wpdb->get_results($query);

    foreach ( $tops as $top ) {
    $return .= '<li><a href="'. esc_url(get_permalink($top->ID)) .'">' . esc_html(get_the_title($top->ID)) .'</a> - '. $top->pageviews.'</li>';
    }

    $return .= "</ul>";

    return $return;
    }
    add_shortcode('notpop', 'NotSoPopV2');
    Thread Starter ehsanmn

    (@ehsanmn)

    thanks for good support

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