使用日期查询及时获取下一张/上一张图像/附件

时间:2015-03-27 作者:Drivingralle

我在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查询中是否有错误?我是否未设置参数?

3 个回复
最合适的回答,由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;
}
也许将过滤器移到上面的函数中会减少代码。但这对我来说更具可读性。

SO网友:Jan Beck

这个get_adjacent_post 函数已经实现了您的要求。它唯一的缺点是使用全局$post变量来确定当前的post。您需要在包装函数中覆盖它,如下所示:

<?php
function get_adjacent_post_by_id( $post_id,  $in_same_term = false, $excluded_terms = \'\', $previous = true, $taxonomy = \'category\' ) {
    global $post;

    $post = get_post( $post_id ); // overwrite global $post variable
    $adjacent_post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );

    wp_reset_postdata(); // reset global $post variable

    return $adjacent_post;

}

// get previous post of post with ID 1337: 
$previous_post = get_adjacent_post_by_id( 1337 );

// get next post of post with ID 1337: 
$next_post = get_adjacent_post_by_id( 1337, false, \'\', false );
?>
我保留了get\\u neighting\\u post函数的所有原始参数。您可以根据需要删除或重新排序它们。

SO网友:Nabeel

Try this out

<?php
/**
 * Get next/preview post by post_date
 * 
 * @param int|WP_Post   $post
 * @param boolean   $next_post
 * @return WP_Post|WP_Error
 */
function get_next_prev_post_by_date( $post, $next_post = true ) 
{
    global $wpdb;

    // get current post data
    if ( !is_a( $post, \'WP_Post\' ) )
        $post = get_post( $post );

    // build select SQL query
    $query = "SELECT * FROM {$wpdb->posts}";
    $query .= " WHERE post_date ". ( $next_post ? \'>\' : \'<\' ) ." %s";
    $query .= " AND ID NOT IN (%d)";
    $query .= " ORDER BY post_date DESC LIMIT 1";

    // run query
    $target_post_row = $wpdb->get_row( $wpdb->prepare( $query, $post->ID, $post->post_date ) );

    // return post object if found
    if ( $target_post_row )
        return new WP_Post( $target_post_row );

    // return error if no match found
    return new WP_Error( \'no_match_found\', __( \'No matched post found\' ) );
}
结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post