Help with sorting custom query

Here is a sample arguments array for the query I am performing:

Array
(
    [post_type] => product
    [orderby] => menu_order
    [order] => DESC
    [nopaging] => 1
    [tax_query] => Array
        (
            [0] => Array
                (
                    [taxonomy] => product-category
                    [field] => id
                    [terms] => Array
                        (
                            [0] => 8
                        )

                )

        )

    [meta_query] => Array
        (
            [0] => Array
                (
                    [key] => product_price_10
                    [value] =>
                    [compare] => !=
                )

        )

)

I am currently ordering by menu_order but I want to actually order by product_price_10

This query is looking for products in a specific category (custom taxonomy). My plugin also has price levels where a single product can have multiple prices, 10 is the ID of the price level currently being queried.

I have tried the following but it returns no results:

Array
(
    [post_type] => product
    [meta_value] => product_price_10
    [orderby] => meta_value_num
    [order] => ASC
    [nopaging] => 1
    [tax_query] => Array
        (
            [0] => Array
                (
                    [taxonomy] => product-category
                    [field] => id
                    [terms] => Array
                        (
                            [0] => 8
                        )

                )

        )

    [meta_query] => Array
        (
            [0] => Array
                (
                    [key] => product_price_10
                    [value] =>
                    [compare] => !=
                )

        )

)
  • Imperative Ideas
    • HummingBird

    If you don't mind my asking, why are you writing a custom array instead of using a wp_query() string?

    <?php
    	$args = array(
    		'orderby'		=> 'meta_value_num',
    		'order'			=> 'ASC',
    		'category'		=> '13',
    		'post_type'		=> 'product',
    		'post_status'		=> 'publish',
    		'posts_per_page'	=> 6,
    		'taxonomy' => 'product_category',
    	);
    
    	$postslist = get_posts( $args );
    	foreach ($postslist as $post) :  setup_postdata($post);
    ?>

    etc, etc, etc, http://codex.wordpress.org/Template_Tags/get_posts, etc

    I assume you have a good reason for re-inventing the wheel, I'm just curious what it is?

  • sccr410
    • Site Builder, Child of Zeus

    This is not a WPMU plugin, this is my own custom plugin.

    I am not reinventing any wheels, I am using the WP_Query class. The array posted originally is the array of arguments I am passing to WP_Query. Here is my current query which simply orders them by the menu_order. I want to make this work so it orders them by the 'product_price_'.$price_level meta_key (again, remember that this is dynamic because of the price levels system I have in my plugin and $price_level is always an integer.

    $args = array(
    						'post_type' => 'product',
    						'orderby' => 'menu_order',
    						'order' => 'DESC',
    						'nopaging' => true,
    						'tax_query' => array(array(
    							'taxonomy' => 'product-category',
    							'field' => 'id',
    							'terms' => array($product_category->term_id)
    						)),
    						'meta_query' => array(
    							array(
    								'key' => 'product_price_'.$price_level,
    								'value' => '',
    								'compare' => '!='
    							)
    						)
    					);
    					$products = new WP_Query( $args );

    This query is asking for all products in a specific category ($product_category->term_id) that have a price set for the also requested price level. I want to order them by price from lowest to highest instead of menu_order.

    When I try the following I get no results back:

    $args = array(
    						'post_type' => 'product',
    						'meta_key' =>  'product_price_'.$price_level,
    						'orderby' => 'meta_key_num',
    						'order' => 'ASC',
    						'nopaging' => true,
    						'tax_query' => array(array(
    							'taxonomy' => 'product-category',
    							'field' => 'id',
    							'terms' => array($product_category->term_id)
    						)),
    						'meta_query' => array(
    							array(
    								'key' => 'product_price_'.$price_level,
    								'value' => '',
    								'compare' => '!='
    							)
    						)
    					);
    					$products = new WP_Query( $args );
  • Jack Kitterhing
    • Code Norris

    Hi there @sccr410

    I hope you are well today.

    Sorry for the delay in my reply, I'm afraid that we don't provide custom coding support.
    However if you could put the entire thing on paste bin i'll take a look, as it's pretty hard to say on this and on your site, as I don't know the rest of your custom plugin or if there's any errors. Have you checked your error log? Do you have define wp_debug set to true?

    Thank you!

    Kind Regards
    Jack.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.