1

I want that whenever a user post something in category "Foo", the same content must be duplicated to the category "Bar" automatically. Is there such a plugin that automate the process? Or some PHP snippet? I have many categories.

Those categories are not the normal WordPress categories, they are custom, for a specific theme, like WooCommerce categories for example. So the plugin must support taxonomies.

The reason is I have a multi-language website, using Polylang plugin, it's user-generated content, most often the content is the same in both languages so it must be the same in both categories. When it's not the case, only then the admin would translate by hand.

The user inserts content always in the English category say "Home category", it must be duplicated automatically to "Casa" category in Spanish, for example.

Note that the second language is Arabic, the first is English, for every English category, there's an equivalent Arabic category, every English post must be inserted in the equivalent Arabic category.

5
  • can't you select both categories when creating the post?
    – inarilo
    Jun 4 '17 at 5:53
  • @inarilo no polylang doesn't allow it, plus I need an automated way
    – Lynob
    Jun 4 '17 at 6:10
  • hi, can you tell me what error you are getting with the code in my answer?
    – inarilo
    Jun 10 '17 at 19:24
  • @inarilo 500 internal server error, i guess it's not your fault, i guess classipress categories insert different stuff into different table, what I'm asking for might be impossible without seeing how classipress creates categories
    – Lynob
    Jun 11 '17 at 17:07
  • @inarilo coz it's classipress categories, not normal wp categories
    – Lynob
    Jun 11 '17 at 17:08
2
+50

Place this in your theme functions:

function mysite_clone_post($post_id, $post, $update) {
  if(!$update && in_category('home cat', $post)) {  //not handled updates and will only run if in this category
      $post_fields = array(  'post_author', 'post_date', 'post_date_gmt', 'post_content',
                             'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type',
                             'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping',
                             'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order',
                             'post_mime_type', 'guid', 'tax_input', 'meta_input');
      $postarr = array();
      foreach($post as $k => $v) if(in_array($k, $post_fields)) $postarr[$k] = $v;            

      $postarr['ID']            = 0;
      $postarr['post_category'] = array('casa cat');
      //not handled post_parent          

      wp_insert_post($postarr);  //not handled errors - returns false on error   
  }
}
add_action('save_post', 'mysite_clone_post', 10, 3);

As mentioned in the comments in the code, I have only handled new posts without post parents, and left out error handling.

I am also assuming there are only two categories and simply assigning the opposite one.

UPDATE:

For custom post types and taxonomies use the following:

function mysite_clone_post($post_id, $post, $update) {
  if(!$update && $post->post_type == 'myposttype' && has_term('home cat name, id or slug', 'mytaxonomy', $post)) {  //not handled updates and will only run if it is this custom post type and in this custom category
      $post_fields = array(  'post_author', 'post_date', 'post_date_gmt', 'post_content',
                             'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type',
                             'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping',
                             'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order',
                             'post_mime_type', 'guid', 'tax_input', 'meta_input');
      $postarr = array();
      foreach($post as $k => $v) if(in_array($k, $post_fields)) $postarr[$k] = $v;            

      $postarr['ID']            = 0;          
      //not handled post_parent          

      if($newid = wp_insert_post($postarr)) {    //if saved successfully, add taxonomy, note that the value is assigned to $newid and not compared
          wp_set_object_terms($newid, 'desired cat id or slug', 'mytaxonomy');
      } else {
          //handle errors here
      }
  }
}
add_action('save_post', 'mysite_clone_post', 10, 3);

In addition, to handle multiple category pairs, you could use an array but then you'd have to update it everytime you add a new category. I'd suggest naming (and using) the slugs in such a way that one can be easily derived from the other. So e.g. 'main_cat' and 'main_cat_ar'.

Then the first line would become:

if(!$update && $post->post_type == 'myposttype') {  //not handled updates and will only run if it is this custom post type
    $terms = get_the_terms($post, 'mytaxonomy');
    if(!is_array($terms) || count(explode('_ar', $terms[0]->slug)) > 1) return;    // if no terms were returned or it belongs to an arabic category, exit

and the line assigning the term to the new post becomes

wp_set_object_terms($newid, $terms[0]->slug.'_ar', 'mytaxonomy');
17
  • where to place that? functions.php?
    – Lynob
    Jun 4 '17 at 9:21
  • yes, updated the answer, also changed the code a bit
    – inarilo
    Jun 4 '17 at 11:27
  • one thing, $post has more properties than are accepted by wp_insert_post, I assume the extra fields will be ignored (by not being accessed) but if there is an error, you may want to just pick up the values you know you need from $post.
    – inarilo
    Jun 4 '17 at 11:49
  • pastebin.com/2gY2XSTT thats my theme function.php, i'm thinking to do it in ` $wpdb->insert( $wpdb->cp_ad_geocodes, array). Also I have many categories, each one have it's equivalent in the other language so perhaps if($postarr['post_category'] == in_category('home cat', $post_id))`
    – Lynob
    Jun 4 '17 at 11:55
  • also my 2nd language is arabic, so i just need to copy the arabic string of the category right?
    – Lynob
    Jun 4 '17 at 11:56

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.