0

I'm going to be setting up a custom post type on a site (for job vacancies) and I'm going to create a separate menu item on the site's main nav using categories, and i'll use a 'jobs' category to effectively have the jobs menu item running as a second blog.

What i would like is to have it so when the client adds a job in the back end, the created post under this custom-post-type automatically has the category 'jobs' applied to it, regardless of any additional categories they use – so the posted jobs automatically get added to this menu item in the front end.

Is there a way of automatically adding a category to a custom-post-type? And also having it so any post with the category 'jobs' doesn't then show on the main blog page (which is going to be a news and market info page).

Any help would be awesome.

Emily

  • 2
    Possible duplicate of Automatic category for a Custom Post Type – Cedon Mar 25 '17 at 1:05
  • @belinus , sorry saw too late your responce but answered and hope it helps her anyway on her way. Btw there they use cat_id which imho is a bad example (think about deleting the category by mistake and then recreating it, then it would having another cat_id and not working anymore!) – Charles Mar 25 '17 at 1:14
  • @Charles No problem. – Cedon Mar 25 '17 at 1:19
2

To answer the first question (pre-populate a CPT with a Cat) you could add following function to functions.php .(make backup first svp)

In the code below movies is the Custom Post Type and horror is the Category.
Please change to own preferences.

/**
 * Set a Category on Custom Post Type
 * 
 * Read more: {@link https://developer.wordpress.org/reference/functions/get_post_type/}
 *            {@link https://codex.wordpress.org/Function_Reference/get_category_by_slug}
 *            {@link https://codex.wordpress.org/Function_Reference/wp_set_object_terms}
 *            
 */
add_action( 'save_post', 'wpse261307_set_cat_on_cpt' );
function wpse261307_set_cat_on_cpt( $post_id )
{
    global $wpdb;

    // Check for correct post type
    if ( get_post_type() == 'movies' ) // Set post or custom-post type name here
    {  
        // Find the Category by slug
        $idObj = get_category_by_slug( 'horror' ); // Set your Category here
        // Get the Category ID
        $id = $idObj->term_id;

       // Set now the Category for this CPT
       wp_set_object_terms( $post_id, $id, 'category', true );
    }

}// end function

--

I would then make a function which hides the meta-box in the back-end for this specific CPT. (so no one can add/change the category, but that is only useful when you use only 1 category pro posting)

--

Then you will need to make a function which hide this specific CPT to be shown on the main blog page. (by using a query to exclude the category ' jobs' on the main blog page) .

/**
 * Exclude CPT with Cat jobs on Landing page
 *
 * Read more: {@link https://codex.wordpress.org/Function_Reference/get_category_by_slug}
 *            {@link https://codex.wordpress.org/Function_Reference/wp_set_object_terms}
 *            {@link https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts#Exclude_categories_on_your_main_page}
 *            {@link https://codex.wordpress.org/Function_Reference/is_main_query#Examples}
 */
add_action( 'pre_get_posts', 'wpse261307_exclude_cat_main_blog' );
function wpse261307_exclude_cat_main_blog( $query )
{   
    global $wpdb;

    // Find the Category by slug
    $idObj = get_category_by_slug( 'horror' );
    // Get the Category ID
    $id = $idObj->term_id;

    if ( $query->is_home() && $query->is_main_query() ) {

        $query->set( 'cat', -$id );
    }

} // end function

Hope this helps you on your way.

Note:
• Add this to a home brew plugin or in a myfunctions.php in the wp-content/mu-plugins folder (which you have to create yourself)
• Imho the most solid solution to be sure nothing get lost (meaning out of sight because it still is in your database) when changing to another theme. (changes made directly in a template file are not anymore visible when you change to another theme)
• Btw, the same (adding them into a myfunctions.php file in the wp-content/mu-plugins folder) I would do for creating Custom Post Types.

Ps, to clarify why I am not a fan of using id numbers directly:

  • It happens that a category is deleted for whatever reason and afterwards it seems a mistake and the same category needs to be created again.
  • When a new category is created (even with the same name) it will get another id number and then (when you use the number itself directly in any code) your functions are not working anymore because of the wrong id number.
0

Auto adding a category to a custom post type was already asked and answered here.

As for excluding it, then you need to write a custom WP_Query:

$args = array( 
   'post_type' => 'post',
   'cat' => -1,
);

$fp_query = new WP_Query( $args )
// The Loop
if ( $fp_query->have_posts() ) {
   // Loop Code for Posts
   wp_reset_postdata();
} else {
  // No Posts Found Code
}

You would change the 1 to whatever number the Category ID for jobs is but keep it negative as this triggers an exclusion.

  • Please see my comment about the not recommending ppl to use a cat_id and your post_type = is also not correct because it will be a custom post type amd not a regular post. Do not take this as a pure negative comment but just as information. – Charles Mar 25 '17 at 1:29
  • You can only do exclusions with the IDs though. At least without writing a custom function to take the slug and return the ID and then feed it into $args as a negative number. – Cedon Mar 25 '17 at 1:33
  • I rather not go into discussion in comments and maybe I am wrong, although I doubt that I am in this case. (please take a look at my answer and the links which explain what I do and why). – Charles Mar 25 '17 at 1:39
  • I'm just going by the documentation for WP_Query – Cedon Mar 25 '17 at 1:43

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.