Support » Plugins » Help with The Loop and replacement query

  • I run a private site with a limited number of users. Each post has a rating feature (via plug-in) attached to it, and I’m trying to create a plug-in (or link, something) to let them display all the posts that they have not yet rated.

    In MySQL, I know exactly what my query has to look like:

    SELECT post.post_title, FROM wp_posts
    LEFT JOIN rating_table AS votes ON AND votes.user_ID=1 WHERE IS NULL;

    The user ID bit would be variable, of course, for each logged in user. This query works fine as raw SQL for finding those posts that a given user hasn’t yet rated.

    However, where my complete lack of experience shows is in how to translate the above into a replacement query to build The Loop’s selection of posts.

    Any and all pointers appreciated; I’ve done tons of googling, but my head just winds up spinning (I’m not a coder by trade, but can usually muddle my way through the basic stuff).


Viewing 1 replies (of 1 total)
  • Information on the old pre-1.5 version of The Loop will guide you on how to build your custom query into it:

    (Ignore the first bit on require('./wp-blog-header.php') unless this is being used in a non-WP PHP document).

    So, how to replace the $posts object in that loop with your own? Here’s an (untested) answer:

    global $wpdb, $user_ID;
    $posts = $wpdb->get_results("SELECT post.post_title, post.ID FROM $wpdb->posts AS post LEFT JOIN rating_table AS votes ON WHERE votes.user_ID=$user_ID AND IS NULL");

    Note that $user_ID is a global var available when a user is logged in.

    If this is run (such as in the sidebar) where it can conflict with other $posts objects, think about changing your $posts to something unique, like say $notrated.

Viewing 1 replies (of 1 total)
  • The topic ‘Help with The Loop and replacement query’ is closed to new replies.