Support » Fixing WordPress » Ordering WP_Query by nav menu items using post__in

  • Ok so basically i want to order my posts so they are displayed in the exact same order as my sidebar menu, this meaning the client won’t have to rearrage the posts in both the sidebar menu and in the page attirbutes.

    I am trying order my posts by using post__in, but my query also uses a tax_query to filter my results by a custom taxonomy.

    my current code is:

    $prod_id = get_queried_object()->term_id; 
    $the_items = wp_get_nav_menu_items ('products-sidebar-menu');
    $new_array = array();
    foreach($the_items as $l) {
         $new_array[] = $l->object_id;	
    $args = array(
         'post_type' => 'product',
         'posts_per_page' => -1,
         'post__in' => $new_array,
         'orderby' => 'post__in',
         'tax_query' => array(
              'taxonomy' => 'product-category',
              'field' => 'id',
              'terms' => $prod_id
    $products = new WP_Query($args);
    //loop starts here

    What’s odd is i think both post__in and tax_query conflict with each other as if i comment out my tax query, the post__in order works absolutely fine (but my results arent filtered to that specific taxonomy), Or vice versa, if i comment out my post__in i get my results filtered perfectly but i don’t get them in the order i want them .

  • The topic ‘Ordering WP_Query by nav menu items using post__in’ is closed to new replies.