admin管理员组文章数量:1125766
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.
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
Reset to default 0It 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.
本文标签: databaseSet limitations of wpupdatepost()
版权声明:本文标题:database - Set limitations of wp_update_post()? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736674224a1947091.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论