Support » Fixing WordPress » Can anyone translate MSSQL/T-SQL to PHP/WP/MySQL?

  • I’d like to use the mini-loop on our home page in a few places to pull up lists of posts only from specific topics. I think I figured out the proper query logic, but I only ‘speak’ T-sql (I’m a person by day).

    I’m not really sure how to turn the mini-loop query into something like this:

    SELECT TOP 5 wp_posts.ID, post_Title FROM wp_posts INNER JOIN wp_post2cat ON wp_post2cat.post_id = wp_posts.ID INNER JOIN wp_categories ON wp_categories.Cat_ID = wp_post2cat.Cat_ID WHERE wp_categories.cat_ID = myVariable

    with myVariable = the particular category ID I’m looking for.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The syntax is the same except remove the top 5 from the beginning and append it as limit 0,5 at the end.

    it’s the same? Oh. Why does the sample look so different, then?

    $news=$wpdb->get_results(“SELECT ID,post_title FROM $wpdb->posts
    WHERE post_status= \”publish\” ORDER BY ‘ID’ DESC LIMIT “.$how_many);

    I guess I’m hung up on the $wpdb->posts

    What does that mean? The same as wp_posts?

    Aha! Ok, so, I realized that if I just use a regular loop, I get access to a new function that I can just pass the category parameter to:

    So, followup question. The loop makes it easy to list one specific category. Is it easy to use the loop to EXCLUDE specific categories (ie, more than one?)

    “Why does the sample look so different, then?”

    Different? Different to what?

    Your initial SQL statement (because you haven’t specified the ORDER BY clause) will obviously return the data in whatever the database server considers the “natural” sequence and that depends on the database.

    I believe that you have to specify the ORDER BY clause regardless of whether you’re using MySQL, MSSQL or anything else.

    all the (built-in) table names are indirected in $wpdb. it might be wp_posts, or could be david_posts, or anything else at that point.. 😉

    pizdin…you are right, the syntax really isn’t any differnt.

    So, cool. It looks like I can just use custom queries then, which will be easier.

    That said, now that I’m using the full LOOP, it doesn’t use a query, but passes variables to a function. Are there any EXCLUDE variables?

    pizdin…the exclude example on that page says ‘v1.5 only’. Do you know if that would work in 2.1 as well?

    Sorry, I don’t. But I would expect that it should.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Can anyone translate MSSQL/T-SQL to PHP/WP/MySQL?’ is closed to new replies.