WordPress.org

Ready to get started?Download WordPress

Forums

MySQL Query for Categories (11 posts)

  1. tvisher
    Member
    Posted 6 years ago #

    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.

  2. moshu
    Member
    Posted 6 years ago #

    Clicking on any category name... WP does it for you: it will show only posts from that category.
    What else do you need?

  3. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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);
  4. tvisher
    Member
    Posted 6 years ago #

    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.

  5. moshu
    Member
    Posted 6 years ago #

    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.

  6. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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.

  7. tvisher
    Member
    Posted 6 years ago #

    OK,

    Here's the basic story. I have a website (http://burningones.com/) 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.

  8. moshu
    Member
    Posted 6 years ago #

    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:

    <?php
    require('./patht-to-your-wpblog/wp-blog-header.php');
    ?>

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

  9. tvisher
    Member
    Posted 6 years ago #

    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.

  10. moshu
    Member
    Posted 6 years ago #

    1. Date and Time tags: http://codex.wordpress.org/Template_Tags#Date_and_Time_tags
    most likely, the_time
    2. Author tags:
    http://codex.wordpress.org/Template_Tags#Author_tags
    3. Post tags:
    the_title
    the_content
    http://codex.wordpress.org/Template_Tags#Permalink_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 :)

  11. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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 otto@ottodestruct.com if you want more info.

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

    <?php
    require('./patht-to-your-wpblog/wp-blog-header.php');
    ?>

    Wherever you're displaying the post:

    <?php
    query_posts('category_name=Main&showposts=1');
    while(have_posts()): the_post();
    the_date();
    the_author_firstname();
    the_author_lastname();
    the_title();
    the_content();
    the_permalink();
    endwhile;
    ?>

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

Topic Closed

This topic has been closed to new replies.

About this Topic