Sort on two variables

时间:2016-10-24 作者:DevIT

我试图让我的列表(优雅主题-eList)按自定义字段排序,然后按名称排序。

我试图包含两个order by语句,但无法对列表进行排序。

在for each语句中,我返回posi

    $posi = get_post_meta($post->ID,\'feature_posi\',true);
我有一个自定义字段,其中填充了1、2、3。

我试图让列表显示1、2、3,然后按标题对其余列表进行排序。

我在下面包含了我的所有代码。

我是一名n00b,正在努力了解PHP排序。

如有任何帮助,将不胜感激。

谢谢

    <section id="recent-listings">
  <div class="container clearfix">
    <?php $et_queried_object = get_queried_object();
?>
      <?php if ( ( isset( $et_queried_object ) && \'listing_tag\' != $et_queried_object->taxonomy ) || is_home() ) {
    ?>
        <?php
}
?>
          <?php
$et_term = get_queried_object();

$et_count = 0;

$et_divider = 1;


if ( is_home() ){

    $et_divider = 1;

}
else {

    echo \'<div id="main_content">\';

    $term_t = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );

    echo \'<h2 class="listing-category-title">\' . $term_t->name . \'</h2>\';


}


$et_divider = apply_filters( \'et_divider\', $et_divider );

$et_open = false;

$term = $wp_query->queried_object;

$zdfs= $term->name;


$term_slug = get_query_var( \'term\' );

$taxonomyName = \'listing_category\';

$current_term = get_term_by( \'slug\', $term_slug, $taxonomyName );



$args = array(
\'post_type\' => \'listing\',
\'posts_per_page\' => -1,
\'orderby\' => \'title\',
\'order\' => \'ASC\',
\'tax_query\'=>array(
array(
\'taxonomy\'=>$taxonomyName,
\'field\'=>$term_slug,
\'terms\'=> get_queried_object()->term_id,
)
)
);


$query = new WP_Query($args);



$posi = get_post_meta($post->ID,\'feature_posi\',true)  ;

if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();

$et_count++;

$et_open = true;

$et_is_last = $et_count % $et_divider == 0;



$z = strip_tags(get_the_term_list( $post->ID, \'listing_category\', \'\', \', \' ));

$terms = wp_get_post_terms( $post->ID, \'listing_category\', \'\' );

$post_terms = array();

foreach ($terms as $post_term) {

    $post_terms[] = $post_term->term_id;
    $posi = get_post_meta($post->ID,\'feature_posi\',true);
    $xlink = get_post_meta($post->ID,\'ExternalLink\',true);
    $thumb = \'\';
                        $width = 230;
                        $height = 100;
                        $classtext = \'post-thumb\';
                        $titletext = get_the_title();
                        $thumbnail = get_thumbnail( $width,$height,$classtext,$titletext,$titletext,false,\'Entry\' );
                        $thumb = $thumbnail["thumb"];


}

$pos = strpos($z, $zdfs);

if ($pos === false) {
}

else {
    ?>

<li>
<img src="<?php echo $thumb ; ?>">
<?php echo $posi ; ?>

<a target="_blank" href="<?php echo $xlink ; ?>" class="link_list<?php echo $posi ; ?>"><?php the_title(); ?></a>



</li>
            <?php
}

if ( ! is_home() && $et_is_last ) {

    if ( is_tax() && ( $term->term_id == $post_terms[0]) ) echo \'<div class="hr"></div>\';

    $et_open = false;

}

?>

              <?php
endwhile;

ob_start();

echo \'<div id="taxonomy_pagination">\';

if ( function_exists(\'wp_pagenavi\') ) {
    wp_pagenavi();
}

else {
    get_template_part(\'includes/navigation\',\'entry\');
}

echo \'</div> <!-- end #taxonomy_pagination -->\';

$et_navigation = ob_get_contents();

ob_end_clean();

endif;

if ( ! is_home() ) {

    if ( $et_open ) echo \'<div class="hr"></div>\';

    if ( isset( $et_navigation ) && ! is_tax() ) echo $et_navigation;

    echo \'</div> <!-- end #main_content -->\';

    get_sidebar();

}

?></div>
  <!-- end .container -->
</section>
<!-- end #recent-listings -->
<?php if ( is_home() && isset( $et_navigation ) ) echo $et_navigation;
?>

1 个回复
SO网友:TheDeadMedic

只要每个列表都有feature_posi 字段(即使是空的),您可以让WordPress/MySQL为您处理订购:

$args = array(
    \'post_type\'      => \'listing\',
    \'posts_per_page\' => -1,
    \'orderby\'        => \'title meta_value_num\',
    \'order\'          => \'ASC\',
    \'meta_key\'       => \'feature_posi\',
    \'tax_query\'      => array(
        array(
            \'taxonomy\' => $taxonomyName,
            \'field\'    => $term_slug,
            \'terms\'    => get_queried_object()->term_id,
        )
    )
);