Support » Plugins » [A little chalenge]cat_ID when term_taxonomy _id is not equal to term_id

  • Resolved SimonJ



    Long story short, I would like to use the Lester Chan’s WP-PostView plugin.

    With this plugin, you’re able to make a top-ten (or top-wathever) of the most viewed posts.

    A function in this plugin, “get_most_viewed_category” is made to make this top-10 in one category, for instance : the most viewed posts in category with id 10…

    To do so, it writes a “viewed” value in postmeta table for each viewed post.

    But there is a problem. It works well if your “term_id” is equal to your “term_taxonomy_id”… I’m really not fluent in PHP/MYSQL, so I can’t explain how and why, but as I understand, the plugin retreive the “term_taxonomy_id” and read if it’s a “category” taxonomy, but it don’t take care about the term_id (wich is the real cat_id)… it just assume that the term_taxonomy_id=cat_id…

    BUT, in my case, as many of us I guess, term_taxonomy_id is not the same number as the term_id… Back in the days, let’s say I created 5 category and deleted the 5th, then created a new one, I have now cat : 1-2-3-4-6…

    So, when I udpated wordpress with the new category/term/taxonomy management, the term_taxonomy_id went offset…

    1 = 1
    2 = 2
    3 = 3
    4 = 4
    5 = 6

    Am I clear ??? 🙂

    MY QUESTION : How could we get it works ? I.E. get the real category ID even if it’s not the same number then the term_taxonomy_id ???

    I just wonder if a WP/PHP/SQL guru can have a look at it and help to improve this plugin to get it fully compatible with 2.3…

    Here is the intersting part of the function’s code :

    if(is_array($category_id)) {
    $category_sql = "$wpdb->terms.term_id IN (".join(',', $category_id).')';
    } else {
    $category_sql = "$wpdb->terms.term_id = $category_id";
    if(!empty($mode) && $mode != 'both') {
    $where = "post_type = '$mode'";
    } else {
    $where = '1=1';
    $most_viewed = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts
    LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID
    LEFT JOIN $wpdb->term_relationships ON $wpdb->term_relationships.object_id = $wpdb->posts.ID
    WHERE post_date < '".current_time('mysql')."'
    AND $category_sql AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = ''
    ORDER  BY views DESC LIMIT $limit");

    I posted the complete function here :


    A way to get it works of course, is to run som query in the database to fit the term_id and the term_taxonomy, then update the term_relationship and so on… But well… :-)…

    If someone have an idea…

    Thanks x 1000


Viewing 1 replies (of 1 total)
  • Just to let you know. If someone is having the same problem, Lester Chan found a fix, you can find it on the plugin’s forum.


Viewing 1 replies (of 1 total)
  • The topic ‘[A little chalenge]cat_ID when term_taxonomy _id is not equal to term_id’ is closed to new replies.