dtpa98038 2015-03-25 19:49
浏览 26
已采纳

为WP_Query参数添加额外的排序层

Active environment where the following will reside: http://test-hdwg.pantheon.io/projects/

Ran into a little PHP wp_query() filtering task that I'd like to know how to best approach. Client would like to filter the results of a WP_Query based on the following (lamen written) algorithm utilizing Advanced Custom Fields (ACF) values within a WordPress (4.1.1) environment:

If the query produces multiple results... {
  sort the results by ACF field “project_year_end” {
    if the field contains “present”, filter these items first {
      if multiple results, then {
        further sort by the field “project_year_start” value {}
      }
    }
    else if, sort those without “present” afterwards by the “project_year_end” value { 
      if multiple results for a single value (ex: 2015), then {
        further sort by “project_year_start” { }
      }
    }
  }
}

Currently, the template (PHP) that needs to include this filtered query is built in the following fashion:

<?php /* Template Name: Projects */ ?>
<?php get_header(); ?>

    <main id="primary" class="content-area">
      <div class="jumbotron">
        <div class="row">
          <div class="intro"><?php the_field('projects_heading') ?></div>
        </div>
      </div>
      <div class="projects">
        <div class="row project-list">
          <div class="column-wrapper">
<?php
  $args = array (
    'post_type'     => 'project', // target the post-type for projects
    'post_per_page' => '20',      // display (at max) (x) at once
    'nopaging'      => true,      // disable automatic pagination
    'order'         => 'ASC',     // display order = ascending
    'orderby'       => 'title'    // organized based on the the_title() of the post
  );
  $projects = new WP_Query( $args );
  if ( $projects->have_posts() ) { 
?>
            <div class="project-listing">
<?php while ( $projects->have_posts() ) {
  $projects->the_post();
?>
              <div class="columns ui-listing-block">
                <hr/>
                <h2><?php the_field('project_title') ?></h2>
<?php if( have_rows('project_date_range') ): ?>
                <span>
<?php while( have_rows('project_date_range') ): the_row(); ?>
<?php the_sub_field('project_year_start'); ?>-<?php the_sub_field('project_year_end'); ?>
<?php endwhile; ?>
                </span>
<?php endif; ?>
              <?php echo project_excerpt(); ?>
                <a href="<?php the_permalink(); ?>" title="Learn more about the project: <?php the_field('project_title') ?>" class="button postfix">Learn More</a>
              </div>
<?php } ?>
            </div>
<?php } else { ?>
            <p>No Projects Available.</p>
<?php } wp_reset_postdata(); ?>
          </div>
        </div>
      </div>
    </main>
<?php get_footer(); ?>`

Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • douyou3619 2015-03-26 15:37
    关注

    You can sort by acf fields using meta_key => 'your_acf_field' then orderby => 'meta_value_num'. See this link for help http://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下