Store Category number with post
-
On my website, I wanted to show bits of my posts on the front page, but only the most recent posts posts in only one category. So I was looking through the database structure to figure out the query that I would need to pass and noticed that there was a field for each post’s category (
post_category
) in the wp_posts table, which was nice. But the problem (not really a bug, unless you want to do what I’m doing specifically) is that for some reason WordPress (2.0) is not storing any information in this field, at any time. Instead, the category of a post is determined by a completely separate table, wp_post2cat. This would make it rather difficult to display the most recent post in _any_ category, and didn’t make much sense to me.It turns out that the oversight arises from the query WordPress uses when creating and editing posts, stored in wp-includes/functions-post.php. None of the functions included either the field
post-category
or the variable$post-cat
, so I modified them (ever so slightly ^^). The new queries are as follows:In the function
wp_insert_post
...
if ($update) {
$wpdb->query(
"UPDATE $wpdb->posts SET
post_author = '$post_author',
post_date = '$post_date',
post_date_gmt = '$post_date_gmt',
post_content = '$post_content',
post_content_filtered = '$post_content_filtered',
post_category = '$post_cat',
post_title = '$post_title',
post_excerpt = '$post_excerpt',
post_status = '$post_status',
comment_status = '$comment_status',
ping_status = '$ping_status',
post_password = '$post_password',
post_name = '$post_name',
to_ping = '$to_ping',
pinged = '$pinged',
post_modified = '".current_time('mysql')."',
post_modified_gmt = '".current_time('mysql',1)."',
post_parent = '$post_parent',
menu_order = '$menu_order'
WHERE ID = $post_ID");
} else {
$wpdb->query(
"INSERT INTO $wpdb->posts
(post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_category, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type)
VALUES
('$post_author', '$post_date', '$post_date_gmt', '$post_content', '$post_content_filtered', '$post_cat', '$post_title', '$post_excerpt', '$post_status', '$comment_status', '$ping_status', '$post_password', '$post_name', '$to_ping', '$pinged', '$post_date', '$post_date_gmt', '$post_parent', '$menu_order', '$post_mime_type')");
$post_ID = $wpdb->insert_id;
}
...And then, later on, almost the exact same query, but in the function
wp-insert-attachment
:...
if ($update) {
$wpdb->query(
"UPDATE $wpdb->posts SET
post_author = '$post_author',
post_date = '$post_date',
post_date_gmt = '$post_date_gmt',
post_content = '$post_content',
post_category = '$post_cat',
post_title = '$post_title',
post_excerpt = '$post_excerpt',
post_status = '$post_status',
comment_status = '$comment_status',
ping_status = '$ping_status',
post_password = '$post_password',
post_name = '$post_name',
to_ping = '$to_ping',
pinged = '$pinged',
post_modified = '".current_time('mysql')."',
post_modified_gmt = '".current_time('mysql',1)."',
post_parent = '$post_parent',
menu_order = '$menu_order',
post_mime_type = '$post_mime_type',
guid = '$guid'
WHERE ID = $post_ID");
} else {
$wpdb->query(
"INSERT INTO $wpdb->posts
(post_author, post_date, post_date_gmt, post_content, post_category, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type, guid)
VALUES
('$post_author', '$post_date', '$post_date_gmt', '$post_content', '$post_cat', '$post_title', '$post_excerpt', '$post_status', '$comment_status', '$ping_status', '$post_password', '$post_name', '$to_ping', '$pinged', '$post_date', '$post_date_gmt', '$post_parent', '$menu_order', '$post_mime_type', '$guid')");
$post_ID = $wpdb->insert_id;
}
...I think it would be wise to try and fix this in future versions (it’s not that hard to fix, just hard to find :P), since it seems to me to make more sense to include the post’s category right alongside the rest of the post data, instead of having to cross-reference it against the other table.
Cheers ^^
- The topic ‘Store Category number with post’ is closed to new replies.