[英]Adding additional layers of ordering to a WP_Query argument
將駐留以下內容的活動環境: http : //test-hdwg.pantheon.io/projects/
碰到一個小小的PHP wp_query()過濾任務,我想知道如何最好的方法。 客戶端希望基於以下(拉面編寫的)算法,利用WordPress(4.1.1)環境中的高級自定義字段 (ACF)值來過濾WP_Query的結果:
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(); ?>`
任何幫助將不勝感激。
您可以使用meta_key =>'your_acf_field'然后按orderby =>'meta_value_num'對acf字段進行排序。 請參閱此鏈接以獲取幫助http://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.