排序在Wordpress元查询中无法正常工作

时间:2018-12-16 18:33:18

标签: wordpress

我在wordpress排序方面遇到问题。由于某种原因,它没有将最低的数字放在顶部,而最高的数字放在底部。例如结果。

500 500 1个 1000 1000 2000

我认为问题是由于sort_priority引起的,但所有优先级都设置为0

已编辑。以下是我用来进行查询的实际PHP代码

<?php

if($post_type == 'cds')
{

    $meta_query = array();

    $meta_query[] = array(
        'key' => 'lender_type',
        'value' => 'CDs',
    );



    // <=
    if(isset($_POST['minimum_deposit']) && !empty($_POST['minimum_deposit']))
    {
        $minimum_deposit = trim($_POST['minimum_deposit']);
        $minimum_deposit = (int) $minimum_deposit;


        $meta_query[] = array(
            'key' => 'minimum_deposit',
            'value' => $minimum_deposit,
            'type' => 'numeric',
            'compare' => '<='
        );

    }


    // desposit_term
    if(isset($_POST['deposit_term']) && !empty($_POST['deposit_term']))
    {
        $deposit_term = trim($_POST['deposit_term']);

        $meta_query[] = array(
            'key' => 'deposit_term',
            'value' => $deposit_term,
            'compare' => 'LIKE'
        );

    }

    // rating
    if(isset($_POST['rating']) && !empty($_POST['rating']))
    {
        $ratings = trim($_POST['rating']);
        $list = explode('-', $ratings);

        if(sizeof($list) == 1)
        {
            $meta_query[] = array(
                'key' => 'rating',
                'value' => $list[0],
                'type' => 'numeric',
                'compare' => '<='
            );
        }

        if(sizeof($list) > 1)
        {
            $meta_query[] = array(
                'key' => 'rating',
                'value' => $list[0],
                'type' => 'numeric',
                'compare' => '>='
            );
        }

    }

    // sort_by
    if(isset($_POST['sort_by']) && !empty($_POST['sort_by']))
    {
        $sort_by = trim(strtolower($_POST['sort_by']));
    }

    if(!isset($sort_by))
    {
         $sort_by = 'apy_rate';
    }

    $ordering = 'DESC';

    if($sort_by == 'minimum_balance_for_apy')
    {
        $ordering = 'ASC';

        $meta_query[] = array(
            'minimum_balance_for_apy' => array(
                'key' => 'minimum_balance_for_apy',
                'compare' => 'EXIST'
            )   
        );

    }

    if($sort_by == 'apy_rate')
    {
        $meta_query[] = array(
            'apy_rate' => array(
                'key' => 'apy_rate',
                'compare' => 'EXIST'
            )   
        );
    }

    if($sort_by == 'minimum_deposit')
    {
        $meta_query[] = array(
            'minimum_deposit' => array(
                'key' => 'minimum_deposit',
                'compare' => 'EXIST'
            )   
        );
    }



    // The Query
    $args = array(
        'post_type' => 'lenders',
        'posts_per_page' => -1,
        'meta_query' => array('relation' => 'AND', $meta_query),
        'orderby' => array(
            'sort_priority' => 'DESC',
            $sort_by   => $ordering
        )
    );



    $the_query= new WP_Query($args);


?>

1 个答案:

答案 0 :(得分:0)

我通过添加'type'=>'numeric'

示例:

i

);

感谢帮助人员。