如何根据元值DESC和POST DATE DESC过滤GET上一个POST函数?

时间:2017-06-28 作者:Nick M.

我的循环显示特色帖子,然后按时间倒序显示帖子。

但是当我使用<?php echo get_previous_post(); ?> 它按相反的时间顺序抓取帖子。

如何向上一个post函数添加过滤器,类似于this 而是通过如下数组进行过滤:\'orderby\' => array( \'meta_value\' => \'DESC\', \'date\' => \'DESC\') ?

<小时/>Code:

<小时>UPDATE: 已清理索引。来自@sMyles和@JackJohansson的帮助后的php循环

索引。php循环:

$args = array(
    \'posts_per_page\'    => - 1,
    \'meta_key\'          => \'meta-checkbox\',
    \'orderby\'           => array( \'meta_value\' => \'DESC\', \'date\'  =>  \'DESC\')

);

$posts = new WP_Query( $args );

if($posts->have_posts() ){

    while( $posts->have_posts() ){

        $posts->the_post();

        // Set to content template by default
        $template = \'content\';

        // Change template to featured when `is_featured` meta has a value
        if(get_post_meta(get_the_ID(), \'meta-checkbox\', \'yes\')){
            $template = \'featured\';
        }

        // Load template part
        get_template_part( $template, get_post_format() );

    }
}
<小时>UPDATE: 根据建议,我添加了使用前一个post函数的位置

上一个Post功能

<div id="next-post">
    <?php $prev_post = get_previous_post();
    if(!empty($prev_post)) {
        echo \'<a class="next-story" href="\' . get_permalink($prev_post->ID) . \'">Next Story</a>\'; 
        echo \'<a class="next-story-title" href="\' . get_permalink($prev_post->ID) . \'" title="\' . $prev_post->post_title . \'">\' . $prev_post->post_title . \'</a>\'; 
    } ?>
</div>
特色帖子功能:

function sm_custom_meta() {
    add_meta_box( \'sm_meta\', __( \'Featured Posts\', \'sm-textdomain\' ), \'sm_meta_callback\', \'post\' );
}
function sm_meta_callback( $post ) {
    $featured = get_post_meta( $post->ID );
?>

<p>
<div class="sm-row-content">
    <label for="meta-checkbox">
        <input type="checkbox" name="meta-checkbox" id="meta-checkbox" value="yes" <?php if ( isset ( $featured[\'meta-checkbox\'] ) ) checked( $featured[\'meta-checkbox\'][0], \'yes\' ); ?> />
        <?php _e( \'Featured this post\', \'sm-textdomain\' )?>
    </label>

</div>
</p>

<?php
}
add_action( \'add_meta_boxes\', \'sm_custom_meta\' );

function sm_meta_save( $post_id ) {

    // Checks save status
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ \'sm_nonce\' ] ) && wp_verify_nonce( $_POST[ \'sm_nonce\' ], basename( __FILE__ ) ) ) ? \'true\' : \'false\';

    // Exits script depending on save status
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return;
    }

    // Checks for input and saves
    if( isset( $_POST[ \'meta-checkbox\' ] ) ) {
        update_post_meta( $post_id, \'meta-checkbox\', \'yes\' );

    } else {
        update_post_meta( $post_id, \'meta-checkbox\', \'\' );
    }

}
add_action( \'save_post\', \'sm_meta_save\' );

2 个回复
SO网友:sMyles

您只需对所有帖子(不仅仅是特色帖子)执行新的完整查询,然后设置orderbymeta_value 只要在循环时检查一下,post meta值是否yes, 并输出特色模板,否则输出标准模板。

https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

更新:可能是这样的:

$args = array(
    \'posts_per_page\'    => - 1,
    \'meta_key\'          => \'meta-checkbox\',
    \'orderby\'           => array( \'meta_value\' => \'DESC\', \'date\'  =>  \'DESC\')

);

$posts = new WP_Query( $args );

if($posts->have_posts() ){

    while( $posts->have_posts() ){

        $posts->the_post();

        // Set to content template by default
        $template = \'content\';

        // Change template to featured when `is_featured` meta has a value
        if(get_post_meta(get_the_ID(), \'meta-checkbox\', true )){
            $template = \'featured\';
        }

        // Load template part
        get_template_part( $template, get_post_format() );

    }
}
您会注意到我将元键更改为is_featured .. 使用下划线而不是连字符是一种很好的做法。

要获取帖子上meta的值,只需使用$post 具有__get PHP魔术方法,如果存在,将返回post meta。

http://php.net/manual/en/language.oop5.overloading.php#object.get

SO网友:sMyles

将此添加到您孩子的主题functions.php 要修改主POST循环查询的文件:

add_action( \'pre_get_posts\', \'smyles_main_query_custom_orderby\' );
/**
 *  Modify main post query sort by meta key and date
 *
 * @param \\WP_Query $query
 */
function smyles_main_query_custom_orderby( $query ) {

    // Verify query is main blog archive
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( \'meta_key\', \'meta-checkbox\' );
        $query->set( \'orderby\', array( \'meta_value\' => \'DESC\', \'date\' => \'DESC\' ) );
    }

}
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_postshttps://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/

你的index.php 文件循环可以如下所示:

if ( have_posts() ) {

    while( have_posts() ) {

        the_post();

        $template = get_post_meta( get_the_ID(), \'meta-checkbox\', true ) ? \'featured\' : \'content\';
        // This could have just been the code below had you used underscores like I previously mentioned
        // $template = $post->meta_checkbox ? \'featured\' : \'content\';

        // Load template part
        get_template_part( $template, get_post_format() );

    }
}

结束

相关推荐

未定义变量:WooCommerce_loop

今天,我更新了woocommerce插件,在我的网站上发现以下错误:未定义变量:woocommerce\\u loop我的循环看起来像(对archive-product template 一年前):<?php $newReleasesCounter = 0; $loop = new WP_Query( $args_new_releases ); while ( $loop->have_posts() ) : $loop->the_post(); global $p