0

This one has been bothering me for a few days now, so I'd really appreciate any help you can give me.

Problem overview:

I have a plugin which automatically creates a post and assigns the category "YouTube" whenever a new video is uploaded to a YouTube channel. This part works fine. However - I'm also wanting the post to be assigned the post type "Video". The plugin doesn't allow for this type of customisation, so this will have to be done in php.

Where I've looked: (included for those who like to say "Have you read the forums?")

WordPress Codex Set Post Format

WordPress Codex In Category

WordPress Stack Exchange 1

WordPress Stack Exchange 2

What I've tried:

A few different attempts aren't getting me any results (not white screen of deathing me either so that's a bonus?)

1: (not sure where I'd need to tell that function to run)

//* If post has category YouTube - set post type to video
function default_post_type($post_id) {
  // check autosave
  if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return $post_id;

    if (in_category('YouTube')) {
        set_post_format($post_id, 'video');
    }
  }
}

2: (mixing it up a little bit, because ... reasons?)

//* If post has category YouTube - set post type to video
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  return $post_id;

  function default_post_type($post_id) {
    if (in_category('YouTube')) {
        set_post_format($post_id, 'video');
    }
  }
}

3: (so then I thought "Why even use a function at all?")

//* If post has category YouTube - set post type to video
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  return $post_id;

  if (in_category('YouTube', $post_id)) {
    set_post_format($post_id, 'video');
  }
}

Appreciate any help - even if you can tell me why what I've done is wrong. Thanks.

1

I'm also wanting the post to be assigned the post type "Video".

You can't just change a post to a different Post Type. There is a difference between "Post Type" and "Post Format".

From your examples I assume you would like to change the "Post Format", as per the codex http://codex.wordpress.org/Function_Reference/set_post_format, you would need to paste that code in functions.php. Then call that function in your template. eg:

<!-- Loop Start here -->

    //* If post has category YouTube - set post type to video
    default_post_type($post_id) {

<!-- Loop End -->
  • Thanks Nath. You're right, I want to change the post format - so I'll look into that solution. Before I accept your answer, one thing to clarify: You say to call the function in the template - but this isn't actually meant to be run on any page. It's more a backend thing so that every time a new post is automatically created by the YouTube plugin with the Category "YouTube" - that particular post is also given the correct post format. So really I need to run the function independantly - that's what the "if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {}" is for I guess. – Robodashy Feb 26 '15 at 3:57
  • I see. I was just basing my answer off of what I read in the codex. I've never actually had to change a post format before haha! – ngearing Feb 26 '15 at 4:11
1

For anyone else having this issue - the working code looks like this (placed in the functions.php file):

function default_post_type($post_id) {
  if (in_category('YouTube')) {
    set_post_format($post_id, 'video');
  }
}
add_action( 'publish_post', 'default_post_type' );

Happy WordPressing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.