WordPress.org

Ready to get started?Download WordPress

Forums

Can anyone translate MSSQL/T-SQL to PHP/WP/MySQL? (11 posts)

  1. aluminumcans
    Member
    Posted 8 years ago #

    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 asp.net 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.

  2. pizdin_dim
    Member
    Posted 8 years ago #

    The syntax is the same except remove the top 5 from the beginning and append it as limit 0,5 at the end.

  3. aluminumcans
    Member
    Posted 8 years ago #

    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);

  4. aluminumcans
    Member
    Posted 8 years ago #

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

    What does that mean? The same as wp_posts?

  5. aluminumcans
    Member
    Posted 8 years ago #

    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:

    http://codex.wordpress.org/Template_Tags/get_posts

    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?)

  6. pizdin_dim
    Member
    Posted 8 years ago #

    "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.

  7. davidchait
    Member
    Posted 8 years ago #

    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.. ;)

  8. aluminumcans
    Member
    Posted 8 years ago #

    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?

  9. pizdin_dim
    Member
    Posted 8 years ago #

  10. aluminumcans
    Member
    Posted 8 years ago #

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

  11. pizdin_dim
    Member
    Posted 8 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic