【问题标题】:WordPress loop ignore first post of three separate meta_valuesWordPress 循环忽略三个独立元值的第一篇文章
【发布时间】:2015-12-30 17:46:35
【问题描述】:

我有一个循环将所有新闻都拉出来,但是 ACF 设置了三个主要故事。这些是主要,次要和第三。如果每个字段只设置一​​个帖子,这将不是问题。但是,客户希望能够只设置一个新的主帖子,而不必担心删除旧帖子。

因此,为了完成这项工作,我试图让循环忽略这三个字段中的第一个,同时显示其余部分以及设置为“否”的其他帖子。

我正在尝试这样的事情,但我只是看不出还有其他方法。

$args = array(
                // 'offset' => 1,
                'posts_per_page' => -1,

                'meta_query' => array(
                    array(
                        'offset' => 1,
                        'key'       => 'main_story',
                        'value'     => 'Secondary',
                        'compare' => 'NOT',
                        )
                    ),

                'meta_query' => array(
                    array(
                        'offset' => 1,
                        'key'       => 'main_story',
                        'value'     => 'Third',
                        'compare' => 'NOT',
                        )
                    ),

                'meta_query' => array(
                    array(
                        'offset' => 1,
                        'key'       => 'main_story',
                        'value'     => 'Main',
                        'compare' => 'NOT',
                        )
                    ),
                );

我知道偏移消除了分页的能力,这很重要,但我看到了https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination,并且被告知了一种解决方法。这部分暂时比较重要。

【问题讨论】:

    标签: wordpress loops metadata advanced-custom-fields


    【解决方案1】:

    这就是我最终无法做到上述情况的方法

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
    
    <?php
    
    $excluded_key = "main_story";
    $excluded_val = array("Main", "Secondary", "Third");
    $exclude_ids  = array();
    
    ?>
    
    <?php
    
    foreach ($excluded_val as $exclude) {
    
        $args = array(
            'posts_per_page'  => 1,
            'order'                     => 'DESC',
            'meta_query'            => array(
                array(
                    'key'               => $excluded_key,
                    'value'             => $exclude,
                    )
                )
            );
    
        $excluded_id = get_posts($args);
    
        foreach($excluded_id as $to_exclude) {
            $exclude_ids[] = $to_exclude->ID;
        }
    }
    
    
    ?>
    
    <?php
    
    $args = array(
        'post__not_in'      => $exclude_ids,
        'paged'                     => $paged
        );
    
        ?>
    
        <?php $the_query = new WP_Query( $args ); ?>
    
        <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    

    【讨论】:

      最近更新 更多