views:

34

answers:

2

I need to amend (via CONCAT, presumably) something to every wordpress post if it belongs to a certain category (say, category ID 7), but I'm struggling to get it to work.

To test, I'm first trying to select all the relevant posts. So far, I have the following:

SELECT post_title 
    FROM cruise_wp_posts 
        LEFT JOIN cruise_wp_term_relationships 
        ON  cruise_wp_term_relationships.object_id = cruise_wp_posts.ID
        WHERE term_taxonomy_id = 87;

However, it only lists posts that are only in category 87 - I need all posts that are in category 87 (and possibly other categories too)

I'm a MySQL newbie, and this is really breaking my brain.

Any pointers would be passionately welcomed.

A: 

Why not just use get_the_category( $id ) and ammend the text when you output the post?

$cat = get_the_category( $postID );
if ($cat == 7) {
  //Add text here
}
Steven
Not a bad idea, and that's what I'm currently doing. However, I would like to learn how to do this properly :-)
palmaceous
As far as I know, this is the proper way. It's much cleaner to use wp functions, rather than working with SQL queries. That's at least my preference.
Steven
Agreed; but that doesn't teach me more about MySQL in the process :-)
palmaceous
WordPress is the wrong place to learn MySQL. You want to use WordPress' APIs as much as possible so that when the core team decides to change the back end your functionality doesn't notice. Direct SQL is easy to break. The APIs are there to be stable and always return predictable results across upgrades.
Gipetto
A: 

The best way to do it is to filter it in as needed. This way the addition is made everywhere the_content is used and not just in the templates you modify.

<?php

function my_content_concat($the_content) {
    if (in_category(7)) {
        $the_content .= '<br /><br />foo!';
    }
    return $the_content;
}
add_filter('the_content', 'my_content_concat', 9);

?>

in_category can take the id, name or slug of your target category.

I put the filter at 9 so that it runs before WordPress texturizes the content. If you don't need that run it at 11.

Gipetto