Content of the page

How to assign filter-category automatically

Content of the article

Assigning of any filter-category automatically (by code in your site) to any post type is necessary when for example a lot of posts should be added by users for example, or by any another reason.  For this should be used WordPress hook save_post

Here is an example how to realize it:

  • open functions.php file of your site current wp theme and drop there next code:
  • function save_mdtf_meta( $post_id, $post, $update ) {
         * In production code, $slug should be set only once in the plugin,
         * preferably as a class property, rather than in each function that needs it.
        $post_type = 'book';
        // If this isn't a 'book' post, don't update it.
        if ( $post_type != $post->post_type ) {
        // - Update the post's metadata.
       update_post_meta( $post_id, 'meta_data_filter_cat', 77 );
    add_action( 'save_post', 'save_mdtf_meta', 10, 3 );
  • 77 here is an example ID of filter-category, and you should write it there correct, test it after the code installation
  • post_type - if you need this operation for only one post-slug.  Comment or remove 'return' in 'if ( $post_type != $post->post_type )' condition