Last active
January 11, 2019 13:09
-
-
Save ville6000/653027d7e532601a3dce3bec17d48c29 to your computer and use it in GitHub Desktop.
Copy and synchronize WordPress posts programmatically with Polylang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$pll = new PLL_Admin( PLL()->links_model ); | |
$pll->add_filters(); | |
$pll_pro = new Polylang_Pro(); | |
$pll_pro->load_modules( $pll ); | |
global $wpdb; | |
$post_type = 'your-post-type'; | |
$total = intval( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = %d", $post_type ) ) ); | |
$per_loop = 50; | |
$max_pages = ceil( $total / $per_loop ); | |
for ( $page = 1; $page <= $max_pages; $page ++ ) { | |
$the_query = new \WP_Query( [ | |
'post_type' => $post_type, | |
'posts_per_page' => $per_loop, | |
'no_found_rows' => true, | |
'paged' => $page, | |
'lang' => 'your-primary-language', | |
] ); | |
if ( $the_query->have_posts() ) { | |
foreach ( $the_query->get_posts() as $item ) { | |
// Loop all languages which to post should be synced to. | |
foreach ( [ 'sv', 'en' ] as $lang ) { | |
$pll->sync_post->copy_post( $item->ID, $lang, true ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment