[1]

joshua
Guest
February 3rd, 2009 at 7:15 pm

My category list is a simple 0-9 and A-Z. Based on the first letter of the title entered in the content_title field, what would be the best way to automatically set the category with the post?

Permalink | Quote

[2]

Mark Cunningham
Guest
February 5th, 2009 at 11:43 am

The best way is to use the “Append to Content” widget and add some code.

So, to clarify, what you want to do is take the first character (in capitals if a letter) of the post’s title and then add it to the category of the same name (i.e. ’0′ to ’9′ and ‘A’ to ‘Z’)? It’s definitely doable.

Try this (I haven’t tested it). Insert this piece of code into an Append to Content widget. Make sure the Append widget is at the end of your post.

<?php
# We want the category name in lowercase as this will be equal to the slug
$title = strtolower(trim(get_the_title($post_id)));
# Get first letter
$firstchar = substr($title,0,1);
$catobj = get_category_by_slug($firstchar);
if($catobj) {
  $catid = $catobj->term_id;
  # Grab existing categories
  $postdata = wp_get_single_post($post_id, ARRAY_A);
  $postcats = $postdata['post_category'];
  # Set the category id
  $postcats[] = $catid;
  # Update post
  $postdata = array(
    "ID" => $post_id,
    "post_category" => $postcats );
  sanitize_post($postdata,"db");
  wp_update_post($postdata);
} ?>

Permalink | Quote

[3]

joshua
Guest
February 5th, 2009 at 5:12 pm

Works like a charm! The only modification I had to make was to remove the following line to prevent the post from keeping the default category it was assigned to.

$postcats = $postdata['post_category'];

Permalink | Quote

[4]

romain_becket
Guest
March 12th, 2009 at 10:17 pm

Good evening,
First of all, I would like to say to the author of this plugin : you are really a genius. Chapeau ! I tried to start a plugin, but really to difficult for me now.

Now I would like only to implement the possibility to reply at a post (to create a correspondence between to user). For that, I post the ID of a post (name : ‘AutosID’) from a page to the ‘reply’ page (make with TDO form plugin).

In my ‘reply’ page, I use the “Append to Content” widget and add some code as you said Mark.

<?php
if(isset($_POST['AutosID'])){
$autosID=$_POST['AutosID'];
$destis=strtolower(trim(get_the_author($autosID)));
add_meta_value($post_ID,’Destis’,$destis);
}

to retrieve and store the name of the author of the first post.
But it doesn’t work. The meta-value isn’t created.
Could you help me ?

Best regards,
Romain

Permalink | Quote

[5]

Mark Cunningham
Guest
March 13th, 2009 at 5:32 pm

Try adding some debug to that:

<php if(isset($_POST['AutosID'])) { ?>
   AutosID set to <php echo $_POST['AutosID']; ?>
<php } else { ?>
   Could not find AutosID!
<php } ?>

This will tell you if you’re getting the AutosID value in correctly.

Permalink | Quote

Advertisments

Join the Discussion