【问题标题】:AND and OR query in WordPress wpdbWordPress wpdb中的AND和OR查询
【发布时间】:2014-02-23 10:16:05
【问题描述】:

对,我有 read through the entire wpdbezSQL,所以我非常了解 wpdb 类可以做什么......本质上 wpdb 之于 SQL 就像 jQuery 之于 Javascript!

因为我以前从未这样做过,所以我不会因为不尝试 SQL 而感到太难过,但这就是我今天所做的,以表明我已经尝试过,只是后来才读到 AND 和 OR 不能使用在 WP_Query 中:\

最终结果应该是,如果用户从 ProductType 下拉菜单中选择 并单击搜索按钮,则页面应根据选择菜单中的该术语返回结果。如果他们从 ProductGroup 中选择一个选项并点击搜索按钮,则需要根据选择返回结果。

如果两个下拉菜单都选择了选项,则 ProductType 和 ProductGroup 都需要查询并返回结果。

    $args = array(

        'post_type' => 'product', 
        'posts_per_page' => -1, 
        'orderby' => 'title', 
        'order' => 'ASC',
        'relation'=>'AND', // or OR
        'relation'=>'OR',  // this is naturally not correct
        'meta_query' => array(
               array(
                   'key'        =>  'product_type',
                   'value'      =>  $ProductType,
                   'compare'    =>  'LIKE'
               ),
               array(
                    'key'       =>  'product_group',
                    'value'     =>  $ProductGroup,
                    'compare'   =>  'LIKE'
                )
            )

    );

        <form name="x" action="" method="GET" class="search-results-form" >
          <select name="productType" class="search-product-type">
                <option value="">Product Type</option>
                <option value="ProductType1">ProductType1</option>
                <option value="ProductType2">ProductType2</option>
          </select>
          <select name="productGroup" class="search-product-type">
                <option value="">Product Type</option>
                <option value="ProductGroup1">ProductGroup1</option>
                <option value="ProductGroup2">ProductGroup2</option>
          </select>
          <input type="submit" value="SEARCH" class="submit-button btn" />
        </form>

我希望这是有道理的,所以如果有人能给我一个起点,我将不胜感激。

【问题讨论】:

    标签: php mysql sql wordpress


    【解决方案1】:

    你可以像这样使用关系条件:--

    $args = array(
        'post_type' => 'product', 
        'posts_per_page' => -1, 
        'orderby' => 'title', 
        'order' => 'ASC',
        'meta_query' => array(
            'relation'=>'AND', // or OR
            array(
                'key'        =>  'product_type',
                'value'      =>  $ProductType,
                'compare'    =>  'LIKE'
            ),
            array(
                'key'       =>  'product_group',
                'value'     =>  $ProductGroup,
                'compare'   =>  'LIKE'
            )
        )
    );
    

    您也可以根据需要更改relation...

    【讨论】:

    • 谢谢 Akshay...我知道我应该为我的页面设置不同的标题。我需要同时使用 AND 和 OR,这显然是不允许的,因此使用 wpdb 编写 SQL 查询
    【解决方案2】:

    我知道这是一个老问题,但万一有人偶然发现(就像我一样),这应该可行:

    <?php
        $args = array(
            'post_type' => 'product', 
            'posts_per_page' => -1, 
            'orderby' => 'title', 
            'order' => 'ASC',
            // 'relation'=>'AND', // Not required, as it defaults to "AND"
            'meta_query' => array()
        );
    
        if (isset($_GET['product_type']) && !empty($_GET['product_type'])) {
            $args['meta_query'][] = array(
                'key'        =>  'product_type',
                'value'      =>  $ProductType,
                'compare'    =>  'LIKE'
            );
        }
        if (isset($_GET['product_group']) && !empty($_GET['product_group'])) {
            $args['meta_query'][] = array(
                'key'        =>  'product_group',
                'value'      =>  $productGroup,
                'compare'    =>  'LIKE'
            );
        }
    ?>
    
    <form name="x" action="" method="GET" class="search-results-form" >
        <select name="productType" class="search-product-type">
            <option value="">Product Type</option>
            <option value="ProductType1">ProductType1</option>
            <option value="ProductType2">ProductType2</option>
        </select>
        <select name="productGroup" class="search-product-type">
            <option value="">Product Type</option>
            <option value="ProductGroup1">ProductGroup1</option>
            <option value="ProductGroup2">ProductGroup2</option>
        </select>
        <input type="submit" value="SEARCH" class="submit-button btn" />
    </form>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 2011-06-16
      • 2018-10-04
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多