-1

What I'm trying to do is similar to this one but we have a bit of difference in getting our info.

Basically, when a user creates a post (under suggestions custom post type) on my site, it is not published right away but is in pending review status. I'm using Gravity forms so user can create posts/suggestions on the frontend. I attached this frontend form on particular pages.

What I want is that upon saving post (pending review) of the user, a category under Suggestions post type will be created depending on the name and slug of the current page where the form is attached. Of course it shouldn't be created again if it's already there (eg. there was another user who first posted suggestion).

For example: The user posted a suggestion on the Actors page (of Movie post type). This page has slug actors. If he is the first to suggest on this page, a category should be created under Suggestions post type with the name Actors and slug actors in which his post (or suggestion) should be saved. If he's not the first to suggest, then there's no need to recreate the category because it was already created before by someone, it just needs to be saved under this category (which can be handled by the Gravity forms so long as the category is existing and not to be created).

I'm not sure of what to put in functions.php. Anyone who can help me with this? I hope I made myself clear here.

  • 1
    Was this resolved? If so then please do accept the answer which helps keep the site tidy. Thanks. – Andy Macaulay-Brook Aug 13 '16 at 11:48
1

The primary thing you'll need to use here is the save_post hook in WordPress to execute your code right after a post is saved. Read up on the save_post hook on the WordPress codex first and then come back here.

The next part is assigning the post to a taxonomy term. This can be done easily with wp_set_post_terms() function.

You'll end up with something basically like the below code that you can stick into your functions.php:

<?php
function save_suggestions_term( $post_id ) {
  // Check the post-type of the post being saved is our type
  $suggestions_post_type_slug = 'suggestion';
  if ( $suggestions_post_type_slug != $_POST['post_type'] ) {
    return; // Not ours - stop
  }

  // You'll need to figure out the name of your category.
  // You'll have $_REQUEST that you can get the fields that were just saved 
  $suggestion_term = 'Actors';

  $taxonomy = 'category'; // The name of the taxonomy the term belongs in
  wp_set_post_terms( $post_id, array($suggestion_term), $taxonomy );

}
add_action( 'save_post', 'save_suggestions_term' );

Note that wp_set_post_terms will create the term if it doesn't already exists or add it to an existing one. This will run each time the post is saved again which means if you change the values of the input fields that are used in creating the taxonomy term name, the taxonomy term will change too.

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.