【问题标题】:Adding additional layers of ordering to a WP_Query argument向 WP_Query 参数添加额外的排序层
【发布时间】:2015-03-26 23:37:02
【问题描述】:

以下内容所在的活动环境:http://test-hdwg.pantheon.io/projects/

遇到了一个小的 PHP wp_query() 过滤任务,我想知道如何最好地处理它。客户希望根据以下(蹩脚编写的)算法过滤 WP_Query 的结果,该算法利用 WordPress (4.1. 1)环境:

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” { }
      }
    }
  }
}

目前,需要包含此过滤查询的模板 (PHP) 采用以下方式构建:

<?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(); ?>`

任何帮助将不胜感激。

【问题讨论】:

  • 我在这里看到的唯一合理的方法是使用usort()
  • 谢谢 Peiter,我已经开始研究 usort()。感谢您的意见。

标签: php advanced-custom-fields wordpress


【解决方案1】:

您可以使用 meta_key => 'your_acf_field' 然后 orderby => 'meta_value_num' 按 acf 字段排序。请参阅此链接以获取帮助http://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/

【讨论】:

  • 谢谢!我会进一步调查!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-01
  • 2012-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多