我在WP中有大量的图片,没有附在任何帖子上。它们按自定义分类法分组(在下面的代码中被忽略)。图像以随机顺序上传到WP,post\\u日期设置为拍摄图像的时间。因此,ID和post\\u日期的顺序不同。
我需要下一个/上一个图像的ID,以便及时浏览抛出时间。
因此,我认为使用WP\\u query的date\\u查询是有意义的。我编写了以下代码:
function get_relative_attachment_id( $this_post_ID, $prev = true ) {
// Set the return var, so it can be overwritten
$attachment_id = null;
// WP_Query arguments
$args = array(
\'post_type\' => \'attachment\',
\'post_status\' => \'any\',
\'posts_per_page\' => \'1\',
\'orderby\' => \'date\',
\'relation\' => \'AND\',
\'post__not_in\' => array( $this_post_ID ),
);
if ( $prev ) {
$args[\'date_query\'] = array(
array(
\'year\' => intval( get_the_date( \'Y\', $this_post_ID ) ),
\'compare\' => \'<=\',
),
array(
\'monthnum\' => intval( get_the_date( \'n\', $this_post_ID ) ),
\'compare\' => \'<=\',
),
array(
\'day\' => intval( get_the_date( \'j\', $this_post_ID ) ),
\'compare\' => \'<=\',
),
array(
\'hour\' => intval( get_the_date( \' G\', $this_post_ID ) ),
\'compare\' => \'<=\',
),
array(
\'minute\' => intval( get_the_date( \'i\', $this_post_ID ) ),
\'compare\' => \'<=\',
),
array(
\'second\' => intval( get_the_date( \'s\', $this_post_ID ) ),
\'compare\' => \'<=\',
),
);
} else {
$args[\'order\'] = \'ASC\';
$args[\'date_query\'] = array(
array(
\'year\' => intval( get_the_date( \'Y\', $this_post_ID ) ),
\'compare\' => \'>=\',
),
array(
\'monthnum\' => intval( get_the_date( \'n\', $this_post_ID ) ),
\'compare\' => \'>=\',
),
array(
\'day\' => intval( get_the_date( \'j\', $this_post_ID ) ),
\'compare\' => \'>=\',
),
array(
\'hour\' => intval( get_the_date( \' G\', $this_post_ID ) ),
\'compare\' => \'>=\',
),
array(
\'minute\' => intval( get_the_date( \'i\', $this_post_ID ) ),
\'compare\' => \'>=\',
),
array(
\'second\' => intval( get_the_date( \'s\', $this_post_ID ) ),
\'compare\' => \'>=\',
),
);
}
// The Query
$query_next_attachment = new WP_Query( $args );
// The Loop
if ( $query_next_attachment->have_posts() ) {
while ( $query_next_attachment->have_posts() ) {
$query_next_attachment->the_post();
// Put the new attachment id into return var
$attachment_id = get_the_ID();
}
} else {
// make the return var null
$attachment_id = null;
}
// Restore original Post Data
wp_reset_postdata();
return $attachment_id;
}
按时间顺序上传图像后,一天内即可返回。但当我开始一天的时候,它开始跳过整整一天。示例(*get returned):
[...]
2015-03-11 04:58 *
2015-03-11 03:58 *
2015-03-11 02:58 *
2015-03-11 01:58 *
2015-03-11 00:58 *
----
2015-03-10 23:58
[...]
2015-03-10 01:58
2015-03-10 00:58 *
----
2015-03-09 23:58
[...]
2015-03-09 01:58
2015-03-09 00:58 *
----
2015-03-08 23:58
[...]
向另一个方向的类似行为<如果日期和ID的顺序不同步,返回的结果会更加混乱。我想先确定“常规”订单。
date\\u查询中是否有错误?我是否未设置参数?
最合适的回答,由SO网友:Drivingralle 整理而成
根据jan becker的回答,我构建了这个片段。这对我很有用:
/*
* Get the next/prev image id inside c-tax: my_snapshot_position_ctax
*/
function my_return_relative_attachment_id( $this_post_ID, $prev = true ) {
global $post;
// overwrite global $post variable
$post = get_post( $this_post_ID );
// filter sql query to work with attachment
add_filter( \'get_next_post_where\', \'my_filter_next_prev_post_where_query\', 10, 3 );
add_filter( \'get_previous_post_where\', \'my_filter_next_prev_post_where_query\', 10, 3 );
// Get the new post object
$adjacent_post = get_adjacent_post( true, array(), $prev, \'my_snapshot_position_ctax\' );
// reset global $post variable
wp_reset_postdata();
// remove sql query filter
remove_filter( \'get_next_post_where\', \'my_filter_next_prev_post_where_query\', 10 );
remove_filter( \'get_previous_post_where\', \'my_filter_next_prev_post_where_query\', 10 );
// check if returned value is a post
if ( ! is_object( $adjacent_post ) ) {
// not a post, return an empty string
return \'\';
}
// return the post id
return $adjacent_post->ID;
}
/*
* Filter to modify sql query because attachments have different post status value
*/
function my_filter_next_prev_post_where_query( $sql_query, $in_same_term, $excluded_terms ) {
// replace \'publish\' with \'inherit\' because we work with attachments
$sql_query = str_replace( \'publish\', \'inherit\', $sql_query );
// return the modified query string
return $sql_query;
}
也许将过滤器移到上面的函数中会减少代码。但这对我来说更具可读性。