2

I have a WP_Query that I'm using for a custom post type filter through AJAX. Every post type has a custom post taxonomy (category), but only one. In the filter process I want the result to be based on the names of the categories, THEN on the names of the products, but I haven't found the right documentation for using methods like usort or ksort with these types of arrays.

Here's my 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
);

So before the $results variable is declared, I want all the arrays to follow the $categories array in an easy way, but haven't a method how to. I might have misinterpreted the documentation completely, but I haven't seen my case appear in the php.net manuals.

5
  • But only one what? Term? Commented May 30, 2016 at 8:58
  • @RutwickGangurde yes. Commented May 30, 2016 at 9:24
  • Try this. stackoverflow.com/questions/17253005/… Commented May 30, 2016 at 9:33
  • @RutwickGangurde thanks, but from what I've interpreted, this is not doable for custom taxonomies. Edit: and I'd be damned, how wrong was I. It worked! Thanks! Commented May 30, 2016 at 12:31
  • Awesome! I'll add this as an answer so that you can accept it. Commented May 30, 2016 at 12:53

1 Answer 1

1

This should work, though I have not tried it.

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

Notice how orderby has 2 parameters separated by a space.

Follow this thread for more info: How to order by two different things at once in a query in WordPress

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.