PHP:使用WP_Query

时间:2016-05-30 08:54:10

标签: php arrays ajax wordpress sorting

我有一个WP_Query,我正在通过AJAX用于自定义帖子类型过滤器。每种帖子类型都有自定义的帖子分类(类别),但只有一种。在过滤过程中,我希望结果基于类别的名称,然后是产品名称,但我没有找到使用usortksort等方法的正确文档使用这些类型的数组。

这是我的PHP:

$args = array(
    'posts_per_page'    => -1,
    'post_type'             => 'product',
    'orderby'                   => 'title',
    'order'                     => 'ASC',
    'tax_query' => array(
        array(
            'taxonomy'  => 'products_tag',
            'field'         => 'slug',
            'terms'         => $taxonomy,
            'operator'  => 'AND',
        ),
    ),
);

$filter_query = new WP_Query($args);
$num_posts = $filter_query->found_posts;

if( $filter_query->have_posts() ):

    while( $filter_query->have_posts() ) : $filter_query->the_post();

        $titles[] = get_the_title();
        $ids[] = get_the_ID();
        $product_categories = get_the_terms( get_the_ID(), 'products_category' );
        $product_category = array_pop( $product_categories );
        $categories[] = $product_category->name;
        $permalinks[] = get_permalink();
        $image_field = get_field('images');
        $images[] = $image_field[0]['url'];
        $descriptions[] = get_field('short-description');

    endwhile;

endif;

wp_reset_postdata();

$response = array(
    'success' => true,
    'titles' => $titles,
    'ids' => $ids,
    'categories' => $categories,
    'permalinks' => $permalinks,
    'images' => $images,
    'descriptions' => $descriptions,
    'taxonomy' => $taxonomy,
    'num_posts' => $num_posts
);

因此,在声明$results变量之前,我希望所有数组都以简单的方式跟随$categories数组,但是没有方法如何。我可能完全误解了文档,但我没有看到我的案例出现在php.net手册中。

1 个答案:

答案 0 :(得分:1)

这应该有效,但我还没试过。

$query = new WP_Query( array( 'post_type' => 'page', 'orderby' => 'title menu_order', 'order' => 'ASC' ) );

注意orderby如何将2个参数用空格分隔。

请点击此主题获取更多信息:How to order by two different things at once in a query in WordPress

相关问题