Support » Fixing WordPress » replacing post2cat MYSQL commands

  • Resolved dizzy100

    (@dizzy100)


    ok i have the following (which i fully understand)

    global $wpdb;
        $rand_posts = $wpdb->get_results("
          SELECT * FROM $wpdb->posts AS p,
          $wpdb->post2cat AS c
          WHERE p.ID = c.post_id
          AND c.category_id = 4
          AND p.post_status = 'publish'
          ORDER BY RAND()
          LIMIT $num"
        );

    But now the new wordpress has moved to the post2tag database fields as well as taxonomy etc i’m completely lost on how to update that (and other parts of my website) to reflect.

    How does a category now link to a post ?

    Can anyone offer any assistance ?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Some info on the new tables:
    http://codex.wordpress.org/index.php?title=Version_2.3:New_Taxonomy

    might review a couple of plugins that have made the 2.3 jump for some hints:
    http://codex.wordpress.org/Plugins/Plugin_Compatibility/2.3

    Unfortunately i’ve read and read that again and again and it confuses the hell out of me 🙁

    Anyone else any suggestions ?

    Did you get a chance to look at how some of the plugins are doing it?

    Well i’m doing that now but ironically 4 of the ones listed so far are calling the categories table even though they say they work no problems and have no reported issues.

    I’m struggling to understand the link on how to find and implement categories and posts together using the above sql. If i could see a working example of how to link posts to the new categories (tags) it would make things a great deal clearer. The codex doesn’t give real life user examples so i’m really in a world of confusion.

    I’ve looked through the new tables and can’t find a logical path to follow.

    I hope someone can explain this as its just got a great deal more complex using wordpress when addressing small sql statements to do rudementary things.

    Front Page Excluded Categories on http://wordpress.org/extend/plugins/ has examples for both the old categories and new terms.

    Okay. Education time. Let’s look at your query.

    SELECT * FROM $wpdb->posts AS p,
          $wpdb->post2cat AS c
          WHERE p.ID = c.post_id
          AND c.category_id = 4
          AND p.post_status = 'publish'
          ORDER BY RAND()
          LIMIT $num

    This is getting random published posts from the category with a number of 4. Right? Now, you want to do something similar, I assume. Since random isn’t (yet) one of the built in selectors (I have submitted a patch though), we’ll need to do a query manually.

    The old way:
    wp_posts held posts.
    wp_categories held categories.
    wp_post2cat linked the two.

    The new way:
    wp_posts holds posts.
    wp_terms holds “terms”, which are basically words.
    wp_term_taxonomy holds the “taxonomy”, which gives these words meaning (like “tag” or “category”).
    wp_term_relationships associates a term_taxonomy with some other object, like posts.

    So, basically you want to do some joins.

    SELECT * FROM $wpdb->posts
    LEFT JOIN $wpdb->term_relationships ON
    ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON
    ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->posts.post_status = 'publish'
    AND $wpdb->term_taxonomy.taxonomy = 'category'
    AND $wpdb->term_taxonomy.term_id = 4
    ORDER BY RAND()
    LIMIT $num

    That assumes that the term numbered “4” is still the category number you’re interested in, it may have changed with the upgrade.

    This is just off the cuff, BTW, there may be syntax errors there or something. 🙂

    Otto42 thats absolutely fantastic and I thank you for both the code snippet and the education.

    I have a lot to learn with taxonomy and although I thought I had it down pat with drupal i guess i’ve just proved i’m not quite as savvy as I think I am.

    Is there anyway to reduce the number of queries used when using RAND() or is it always sporadic and the nature of the thing ?

    Again mate I thank you greatly. This works perfectly and really helps me understand.

    I’m struggling with a similar transition. Wondering if Otto42 or someone else would offer a 2.3 rewrite of this post2cat query so I can get my site functioning in 2.3?

    <?php $postinfo = $wpdb->get_results
    (“SELECT * FROM $wpdb->post2cat
    WHERE post_ID = $post->ID
    ORDER BY category_ID”); ?>

    <?php $data = $postinfo[0]; ?>

    I use this in my single.php file to extract the lowest number cat ID, to determine the appropriate single post template in a bilingual site. Now, can’t display anything with post2cat gone.

    Thanks

    Here’s what the beginner came up with. First, to reiterate from above.

    1. The Category ID is now stored as the attribute term_id in the new $wpdb->term_taxonomy table. This new $wpdb->term_taxonomy table also contains the attribute term_taxonomy_id.

    2. You can use this term_taxonomy_id to relate to your post’s ID via the new $wpdb->term_relationships table. This new table also contains the term_taxonomy_id as well as the attribute object_id. object_id is the same value (analogues to) your post’s ID.

    So, to get the database to find the term_id (your category id) have it:

    1. Search the $wpdp->term_relationships table for the object_id that matches your post’s ID.

    2. Use the term_taxonomy values it finds with the object_id to then search for the same term_taxonomy_ids in the $wpdb->term_taxonomy table to locate the term_ids (your category IDs).

    The code that works for me is:

    <? $mypost = $post->ID ?>
    <?php $terminfo = $wpdb->get_results(“SELECT term_id
    FROM $wpdb->term_relationships, $wpdb->term_taxonomy
    WHERE object_ID = $mypost AND $wpdb->term_taxonomy.term_taxonomy_ID = $wpdb->term_relationships.term_taxonomy_ID
    ORDER BY term_ID”); ?>

    <?// get the first term_id and assign it to $cat?>
    <?php $termrecord = $terminfo[0]; ?>
    <?php $cat = $termrecord->term_id; ?>

    can someone help me swap this over:

    $query = "SELECT post_id FROM $wpdb->term WHERE category_id = '" . $category . "'";

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘replacing post2cat MYSQL commands’ is closed to new replies.