Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and update "keep original publish date" snippet in the docs #1160

Merged
merged 5 commits into from
Dec 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions docs/snippets.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,27 @@ add_action( 'plugins_loaded', function() {
*
* This filter is used to filter the arguments sent to the remote server during a push. The below code snippet passes the original published date to the new pushed post and sets the same published date instead of setting it as per the current time.
*/
add_filter( 'dt_push_post_args', function( $post_body, $post ) {
$post_body['post_date'] = $post->post_date;
add_filter( 'dt_push_post_args', function( $post_body, $post, $args, $connection ) {

// When pushing to an external connection, we use the REST API, so the name of the field is `date`.
// But when pushing to an internal connection, the attributes are sent to wp_insert_post, which expects `post_date`.
$field_prefix =( $connection instanceof \Distributor\ExternalConnections\WordPressExternalConnection ) ? '' : 'post_';

$post_body[ $field_prefix . 'date'] = $post->post_date;
$post_body[ $field_prefix . 'date_gmt'] = $post->post_date_gmt;

return $post_body;
}, 10, 2 );
}, 10, 4 );

/**
* This filters the the arguments passed into wp_insert_post during a pull
*/
add_filter( 'dt_pull_post_args', function( $post_array, $remote_id, $post ) {
$post_array['post_date'] = $post->post_date;
$post_array['post_date_gmt'] = $post->post_date_gmt;

return $post_array;
}, 10, 3 );
```

### Automatically unlink posts
Expand Down
Loading