0

I use Polylang on my site I set this function wp_update_post() to update two-time fields for my all Post Language when I click to update and publish again defualt post. I'm trying to use the following code:

function update_post($post_ID){
$mpostid = pll_get_post( $post_ID, pll_default_language() );
$timeD = current_time('mysql'); 
if($post_ID == $mpostid)
{
    $languages = pll_get_post_translations($mainpostids);
    foreach (array_slice($languages, 1) as $language) {
$updatess = array( 'ID' => $language, 'post_date' => $timeD, 'post_date_gmt' => get_gmt_from_date( $timeD ) );
wp_update_post( $updatess );
                
  } 
}
}

it's working fine but the problem is that it also updates other fields like our one check box subtopic when it's checked and another language post unchecked. when I update the post it also checks this box in all other languages post this thing I don't want. I want only to update the post_date and post_date_gmt of all language posts. please tell me how to limit it.

1 Answer 1

0

It sounds like you're facing an issue where using wp_update_post() in your function is unintentionally altering additional fields beyond the post_date and post_date_gmt. This can happen because wp_update_post() is designed to handle a complete post update, and it might modify other fields if they are not specified or handled correctly in your update array.

To limit the update to only post_date and post_date_gmt, you should ensure that your update array contains only these two fields. However, since you're experiencing issues with other fields being altered, it suggests there might be some interference from other hooks or the way Polylang manages synchronized fields.

Here are a few steps you can take to troubleshoot and resolve this issue:

Disable Unwanted Synchronization in Polylang: Polylang has synchronization settings that might cause additional fields to be synchronized when you update a post. Check your Polylang settings to ensure that only the fields you want are being synchronized across translations.

Modify the Function: Ensure that your function explicitly specifies only the fields you want to update. This would look something like this:

function update_post($post_ID){
    $mpostid = pll_get_post( $post_ID, pll_default_language() );
    $timeD = current_time('mysql'); 
    if($post_ID == $mpostid) {
        $languages = pll_get_post_translations($mpostid);
        foreach (array_slice($languages, 1) as $language) {
            $updatess = array(
                'ID' => $language, 
                'post_date' => $timeD, 
                'post_date_gmt' => get_gmt_from_date( $timeD ),
                'edit_date' => true
            );
            wp_update_post( $updatess, true );
        } 
    }
}

Note the use of 'edit_date' => true in the $updatess array. This tells WordPress to only update the date fields.

Avoid Automatic Synchronization: If Polylang is automatically synchronizing fields you don't want to change, consider temporarily disabling these synchronization features while running your update function, then re-enabling them afterward.

Use Custom SQL Query: If the standard WordPress functions continue to cause issues, you can directly update the database using a custom SQL query. This approach requires caution and a good understanding of the WordPress database structure. Here's a rough example:

global $wpdb;
$languages = pll_get_post_translations($mpostid);
foreach (array_slice($languages, 1) as $language) {
    $wpdb->update( 
        $wpdb->posts, 
        array( 
            'post_date' => $timeD, 
            'post_date_gmt' => get_gmt_from_date( $timeD )
        ), 
        array( 'ID' => $language )
    );
}

This direct database approach bypasses the usual WordPress hooks and functions, so it's less likely to trigger unintended changes.

Remember to back up your database before making direct changes, and test your function thoroughly in a development environment to ensure it behaves as expected.

1
  • thanks for your help its possible i return false to update post meta through this function wp_update_post(); because i figured out that our problem occurs when its also updates post_meta. so i want to stop it
    – SS Apps
    Commented Jan 28 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.