wp query - WP_Query order by not working properly due to category I believe

admin2025-04-19  1

I made a query where it should be ordered by the field external link then by price field the problem it seems that some of the post have multiple category can't seem to figure out why it is not going by order of external link fiend then by price field.

    $query = new WP_Query( array(
        'post_type'         => post,            
        'posts_per_page'    => -1,
        'meta_query'     => array(
            'relation' => 'AND',
            'price_clause' => array(
                'key' => 'price',
                'compare' => 'EXISTS'
            ),
            'external_clause' => array(
                'key' => 'external_link',
                'compare' => 'EXISTS'
            )
        ),

        'orderby'    => array(
            'external_clause' => 'DESC',
            'price_clause' => 'DESC',
        ),
        'tax_query' => array(array(
                            'taxonomy' => 'category',
                            'field'    => 'slug',
                            'terms'    => $category,
                        ),) 
    ) );

I made a query where it should be ordered by the field external link then by price field the problem it seems that some of the post have multiple category can't seem to figure out why it is not going by order of external link fiend then by price field.

    $query = new WP_Query( array(
        'post_type'         => post,            
        'posts_per_page'    => -1,
        'meta_query'     => array(
            'relation' => 'AND',
            'price_clause' => array(
                'key' => 'price',
                'compare' => 'EXISTS'
            ),
            'external_clause' => array(
                'key' => 'external_link',
                'compare' => 'EXISTS'
            )
        ),

        'orderby'    => array(
            'external_clause' => 'DESC',
            'price_clause' => 'DESC',
        ),
        'tax_query' => array(array(
                            'taxonomy' => 'category',
                            'field'    => 'slug',
                            'terms'    => $category,
                        ),) 
    ) );
Share Improve this question edited Nov 15, 2019 at 21:09 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Nov 15, 2019 at 15:32 Mark AnthonyMark Anthony 359 bronze badges 2
  • 1 Does the sort work properly when testing without the tax query? – jdm2112 Commented Nov 15, 2019 at 16:25
  • Also not working properly and it seems the problem is the meta query but can't figure out. – Mark Anthony Commented Nov 15, 2019 at 17:14
Add a comment  | 

1 Answer 1

Reset to default 1

I manage to fixed this the problem was type => 'numeric'

$query = new WP_Query( array(
        'post_type'         => post,            
        'posts_per_page'    => -1,

        'category_name'     => $category,


        'meta_query'     => array(
            'relation' => 'AND',
            'price_clause' => array(
                'key' => 'price',
                'type' => 'numeric',
                'compare' => 'EXISTS'
            ),
            'external_clause' => array(
                'key' => 'external_link',
                'compare' => 'EXISTS'
            )
        ),

        'orderby'    => array(
            'external_clause' => 'DESC',
            'price_clause' => 'DESC',
        ),

    ) );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745000817a279213.html

最新回复(0)