Support » Fixing WordPress » MySQL Query for Categories

  • Hello Everyone,

    I just got done installing WordPress 2.3, moving from bBlog. I’m really happy with it so far, but in order to fully integrate it into my site I need to be able to run queries on it where categories match only certain types. I was browsing through the Database via phpMyAdmin and I noticed that for every single post the only value for post_category is 0.

    So: the question. How can I query the database so that I’m only pulling data for certain categories? I clearly cannot depend on the post_category field, unless there is something wrong with phpMyAdmin where it’s not displaying the field correctly.

    What do you all think?

    Thanks in advance!

    Timmy V.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Clicking on any category name… WP does it for you: it will show only posts from that category.
    What else do you need?

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    in order to fully integrate it into my site

    First, ask yourself, what do I really need to do? I mean, do you *really* need to run direct queries on the database? Or could you include the WordPress functions into your own code and then use them to retrieve the data you need instead? That would be better than directly querying the thing.

    Still, if you absolutely *must* query it directly…

    SELECT * FROM wp_posts
    INNER JOIN wp_term_relationships
    ON (wp_posts.ID = wp_term_relationships.object_id)
    INNER JOIN wp_term_taxonomy
    ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
    AND wp_term_taxonomy.taxonomy = 'category'
    AND wp_term_taxonomy.term_id = CATGEORY_ID_NUMBER_HERE);

    I suppose that the only reason I’m heading in this direction is because this is how I did it with bBlog. I see it as a quick solution that I can duct-tape on while I try to come up with the time to figure out which wordpress functions I would need to use, etc. etc.

    If anyone wants to point me to the file I should look at in the WordPress codebase then I’d be more than happy to do it that way, as this way is turning out to be a little more work than it was with bBlog.

    Thanks for the responses.

    Timmy V.

    Wp displays your “front-end” using the theme system, and the template files in any theme are using the Template_Tags to call different functions defined in the core files.
    Normally, you don’t have to mess with the core files ever.

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    tvisher: We might be able to help you better if we knew what you were actually trying to do. Describe the layout of the site, your goal, how you’re trying to accomplish it, etc. WordPress is generally easy to use, but it will do what you want more easily when you fit what you want into how it works. We can tell you how to do that if you tell us what it is that you want it to do.


    Here’s the basic story. I have a website ( with a lovingly crafted look and feel and lots of custom code involved in it. However, I’m not nearly good enough at web programming to come up with my own blogging software so I want to be able to have blogging software that I can use to manage my blog, but I don’t want to use the software as the main means of displaying it.

    So, I have the blog set up with a few different categories. I would like the main page to display the most recent post in the “Main” category. I would also like a few other categories to be able to display information on my front page. Once I’m done with this, I can use the software and still have my look and feel. After this is done, I’m given a little breathing room to figure out how to Template WordPress so that it looks like the rest of the site. My end-goal would be to have WordPress seamlessly integrated into the website so that all of the flashiness of WordPress would actually be taken away and it would just look like my site. This is going to take me a ton of time as I have very little time to work on the site (< 4 hours a week) so until then I’m cool with just having the frontpage look right but have people kicked out to a WordPressy page until I can get the Template right. Then, of course, I’ll eventually totally strip WordPress Presentation out and just keep its admin interface (which is really awesome).

    Hope that all helps. It’s a bit rambly, but I think I got what I want to do across. Let me know if anything’s unclear.

    Timmy V.

    Otto knows more about coding… I am just an “advanced” WP user 🙂

    But it seems to me you are a bit overcomplicating it.

    Any WP function can be used in php files outside of WP if you make that PHP file “aware” of WP, e.g. having this on the top of the file, before everything:


    Next, making a theme out of an existing html/css design – it should be a piece of cake for a coder:

    This is true.

    So, with a specific goal like this:

    retrieve the post date, author first name, author last name, post title, full contents, and permalink of the latest entry in the "main" section

    What functions might I call from the WordPress codebase? The more I look at the code the more it seems to be a web. I just need something quick until I can take the time to understand it more, know what I mean?

    Thanks in advance!

    Timmy V.

    1. Date and Time tags:
    most likely, the_time
    2. Author tags:
    3. Post tags:

    Everything is there.
    Open the index.php file in the classic theme (it’s the simplest one) and see how they are applied.

    And don’t forget, for anything to be displayed in a template file, you need The_Loop.

    Again, what Otto said: don’t try to force WP to do things according to your preconceptions (the usual mistake users do when switching from something else)! Learn how WP does everything and use its features in your advantage… and it will be a walk in the park 🙂

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    1. I think that it would be simpler and quicker to make a WordPress Template that looks like your site than you think. Templates/Themes for WordPress are *really* easy. Especially when you have an existing site, because all you really do is cut up the HTML into pieces, insert some minor code to make those pieces dynamic, and there you go. Email me at if you want more info.

    2. What you want is sorta like this:
    At the top of your PHP page:


    Wherever you’re displaying the post:

    while(have_posts()): the_post();

    Obviously, you’d spruce that up a bit, but hopefully you get the general idea.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘MySQL Query for Categories’ is closed to new replies.