admin管理员组文章数量:1420104
I want to be able to loop through my posts, but for every post to this:
1) check if some custom field is true or false.
2) if is true just display the post's data (the_title, the_content...).
3) if is false display the same structure of data (the_title, the_content...) but with the pervious revision of this post.
Is it possible? and how?
I want to be able to loop through my posts, but for every post to this:
1) check if some custom field is true or false.
2) if is true just display the post's data (the_title, the_content...).
3) if is false display the same structure of data (the_title, the_content...) but with the pervious revision of this post.
Is it possible? and how?
Share Improve this question asked Oct 30, 2016 at 16:53 BenBen 3691 gold badge5 silver badges15 bronze badges2 Answers
Reset to default 2First we look into the wp_get_post_autosave
function
It's informative to see how the core function wp_get_post_autosave()
uses
the wp_get_post_revisions()
function.
It loops over all revisions from
$revisions = wp_get_post_revisions(
$post_id,
array(
'check_enabled' => false
)
);
and then uses:
foreach ( $revisions as $revision ) {
if ( false !== strpos( $revision->post_name, "{$post_id}-autosave" ) ) {
if ( $user_id && $user_id != $revision->post_author )
continue;
return $revision;
}
}
to return the first revision with a slug containing "{$post_id}-autosave"
and where the $user_id
possibly matches it's author.
Alternative
Now wp_get_post_revisions()
is a wrapper for get_children()
, so I wonder why it has to fetch all the revisions for the given post, before filtering out a single one. Why not try to fetch it directly, only what's needed? When we try e.g. the following (PHP 5.4+):
$revisions = wp_get_post_revisions(
$post_id,
[
'offset' => 1, // Start from the previous change (ofset changed to offset)
'posts_per_page' => 1, // Only a single revision
'name' => "{$post_id}-autosave-v1",
'check_enabled' => false,
]
);
then the posts_per_page
will not have any effect. After playing around with this I managed to get the following to work with the posts_per_page
argument:
$revisions = wp_get_post_revisions(
$post_id,
[
'offset' => 1, // Start from the previous change
'posts_per_page' => 1, // Only a single revision
'post_name__in' => [ "{$post_id}-autosave-v1" ],
'check_enabled' => false,
]
);
Get only the previous revision
Now we can adjust the above to only fetch the previous revision, i.e. that's not an auto-save:
$revisions = wp_get_post_revisions(
$post_id,
[
'offset' => 1, // Start from the previous change
'posts_per_page' => 1, // Only a single revision
'post_name__in' => [ "{$post_id}-revision-v1" ],
'check_enabled' => false,
]
);
Here we target the {$post_id}-revision-v1
slug.
Note the here we use the v1
, but we could adjust that if needed later on.
Example
So to finally answer your question, here's a suggestion:
$show = get_post_meta( get_the_ID(), 'somekey', true );
if( $show )
{
// Template part
get_template_part( 'template-parts/content' );
}
else
{
// Fetch the previous revision only
$revisions = wp_get_post_revisions(
get_the_ID(),
[
'offset' => 1, // Start from the previous change
'posts_per_page' => 1, // Only a single revision
'post_name__in' => [ sprintf( "%d-revision-v1", get_the_ID() ) ],
'check_enabled' => false,
]
);
if( $revisions )
{
$post = reset( $revisions );
setup_postdata( $post );
// Template part
get_template_part( 'template-parts/content' );
wp_reset_postdata();
}
else
{
// Some fallback here
}
}
Hopefully you can adjust it further to your needs!
Yes it's possible. You can access to post's revisions using wp_get_post_revisions($post_id)
function. It returns array of post's revision, first element of an array is the same as current version of post so you should take second's element values.
本文标签: loopIs it possible to display previous post revision
版权声明:本文标题:loop - Is it possible to display previous post revision? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745326935a2653634.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论