Cannot order by in WP_Query

时间:2014-12-18 作者:Neovea

我陷入了这个问题:我有一个自定义查询,在其中我不能按任何东西排序。我想按特定的meta\\u值排序。但当按姓名、头衔、日期等排序时,我仍然不能。我做错了什么?

<?php 
    // Get the parent cat thanks to the page\'s cat slug
    $parent_cat = get_category_by_slug( $cat[1] );
    $args = array(
        \'type\'      => \'post\',
        \'child_of\'  => $parent_cat->term_id
    );
    // child cats
    $categories = get_categories( $args );
    // loop on the child cats to get the sub cats object
    if ($categories) {?>
    <!-- THE TITLE -->  
    <div class="text-seperator">
        <h2>Sector\'s programs</h2>
    </div>
    <ul class="sc_accordion">
    <?php
        foreach ($categories as $key => $value) {
            $args = array(
                \'post_type\' => \'editorial-posts\',
                \'meta_key\' => \'meta_post_order\',
                \'orderby\' => \'meta_value\',
                \'order\' => \'DESC\',
                \'tax_query\' => array(
                    \'relation\' => \'AND\',
                    array(
                        \'taxonomy\' => \'category\',
                        \'field\' => \'slug\',
                        \'terms\' => array( $value->slug ),
                        \'include_children\' => false
                    ),
                    array(
                        \'taxonomy\' => \'post_tag\',
                        \'field\' => \'slug\',
                        \'terms\' => array( \'short-description\' )
                    )
                )
            );
            $program_desc_short = new WP_Query( $args );
            ?>
            <?php if ($program_desc_short->have_posts()) : $program_desc_short->the_post() ?>
                <?php 
                //var_dump( get_post_meta( get_the_ID() ) );
                $metas = get_post_meta( get_the_ID(), \'meta_post_order\' );
                $order = $metas[0];
                ?>
                <li><?php echo $order ?><a class="sc_accordion-btn" href="#"><?php echo $value->name ?></a>
                <div class="sc_accordion-content"><?php the_content(); ?>
                <?php edit_post_link(__(\'Edit This\'),\'<p class="edit-link">\' ,\'</p>\'); ?>
                <a href="<?php echo \'/sectors/\'.$value->slug ?>" class="btn small-btn right">Read more</a>
                </div>
            <?php endif;
            wp_reset_query();
        }
    ?>
    </ul>
    <?php 
    }                                    
?>
谢谢你的帮助

1 个回复
SO网友:Neovea

好吧,问题是因为我在类别上循环以获得匹配的帖子。我要做的是得到所需的类别,并在我的$args + 一些调整。

<?php 
    // Get the parent cat thanks to the page\'s cat slug
    $parent_cat = get_category_by_slug( $cat[1] );
    $cats_args = array(
        \'type\'      => \'post\',
        \'child_of\'  => $parent_cat->term_id
    );
    // child cats
    $categories = get_categories( $cats_args );
    // loop on the child cats to get the sub cats object
    if ($categories) {
        // empty array to store cats
        $cats = array();
        // Get the cat slugs and store it in an array
        foreach ($categories as $key => $value)
            $cats[] = $value->slug;
        ?>
    <!-- THE TITLE -->  
    <div class="text-seperator">
        <h2>Sector\'s programs</h2>
    </div>
    <ul class="sc_accordion">
    <?php
        $args = array(
            \'post_type\' => \'editorial-posts\',
            \'tax_query\' => array(
                \'relation\' => \'AND\',
                array(
                    \'taxonomy\' => \'category\',
                    \'field\' => \'slug\',
                    \'terms\' => $cats,
                    \'include_children\' => false
                ),
                array(
                    \'taxonomy\' => \'post_tag\',
                    \'field\' => \'slug\',
                    \'terms\' => array( \'short-description\' )
                )
            )
        );
        $program_desc_short = new WP_Query( $args );
        ?>
        <?php if ($program_desc_short->have_posts()): while ($program_desc_short->have_posts()) : $program_desc_short->the_post() ?>
            <?php 
            $terms = get_the_terms( get_the_ID(), \'category\' );
            $attached_category = array();
            foreach ($terms as $term)
                // in order to only get the concerned category, we check that the term is matching the cats array
                if (in_array($term->slug, $cats))
                    foreach ($term as $key => $value)
                        $attached_category[$key] = $value; 
            ?>
            <li><a class="sc_accordion-btn" href="#"><?php echo $attached_category[\'name\'] ?></a>
            <div class="sc_accordion-content">
                <?php the_content(); ?>
                <?php edit_post_link(__(\'Edit This\'),\'<p class="edit-link">\' ,\'</p>\'); ?>
                <a href="<?php echo \'/sectors/\'.$attached_category[\'slug\'] ?>" class="btn small-btn right">Read more</a>
            </div>
        <?php 
        endwhile; 
        endif;
        wp_reset_query();
    ?>
    </ul>
    <?php 
    }                                    
?>

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post