ACF Wordpress & sort category archive page


ACF Wordpress & sort category archive page

我正在使用acf wordpress插件将布尔字段(featured_project)添加到自定义post(project)中。

我正在尝试对类别存档页面上的帖子进行排序,以显示顶部精选和底部未精选的帖子。

具有相同类型帖子的多个类别。

我读过其他一些类似的问题,其中解决方案是使用"wp_query"或pre_get_posts但我似乎无法让它工作。

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php if ( get_field('featured_project') ) { ?>
        <article class='project featured' id="post-<?php the_ID(); ?>" <?php post_class( 'cf' ); ?> role="article">
        <a href='<?php the_permalink(); ?>'>
           <h3 class="h2"><?php the_title(); ?></h3>
           <?php    
                $url =  wp_get_attachment_url( get_post_thumbnail_id() );      
                $width = 300;                                                                  
                $height = 200;                                                                 
                $crop = true;                                                                 
                $retina = false;                                                             
                // Call the resizing function (returns an array)
                $image = matthewruddy_image_resize( $url, $width, $height, $crop, $retina ); 
           ?> 
           <img src='<?php echo $image['url']; ?>'/ alt='<?php the_title(); ?>'>
        </a>    
        <?php // the_post_thumbnail( 'projects-full', false ); ?> 
        <div class='excerpt'><?php the_excerpt(); ?></div>
        </article>
        <?php } else { ?>

是类别文件中代码的一部分。

谢谢

更新:仍然没有找到解决方案,其他人可以插话吗?

好吧,问题和解决方案不在这个代码块上......问题出在查询中,要解决此问题,您还应该查询元键

<?php
$args = array_merge( $query, 
    array( 
        'meta_query' => array(
        'relation' => 'AND',
            array(
                'key' => 'featured_project',
                'value' => 1, // test if your value is a number or string
                //'type' => 'BINARY', //Maybe you should cast the meta value
                'compare' => '='
                ),
            ),
        ) 
    );
    $custom_query = new WP_Query( $args ); ?>
?
<?php if ($custom_query->have_posts()) : while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
    <?php if ( get_field('featured_project') ) { ?>

也许您还应该在查询中添加 orderby(类似于 'orderby' => ' meta_value_num date'