簡體   English   中英

如何從同一單個列和表中搜索兩個元值

[英]How to search two meta values from same single column and table

我在WordPress中進行自定義搜索。 有三個字段可供搜索。

  1. 年齡
  2. 位置
  3. 帖子標題

在此處輸入圖片說明

wp_posts > post_title字段搜索帖子標題,位置和年齡是自定義元字段y_age, y_activity_locations 因此,有6種搜索場景。

如果用戶輸入:

  1. 輸入標題或
  2. 選擇位置或
  3. 選擇年齡或
  4. 輸入標題並選擇位置或
  5. 輸入標題並選擇年齡或
  6. 選擇位置並選擇年齡

我的前5個(五個)方案工作正常,但我的第6個方案卻無法工作,因為它來自同一表( wp_postmeta )和同一列,但有兩個不同的值。

所以我嘗試了這個查詢:

Select y_posts.*, y_meta.*
From wp_posts As y_posts
Inner Join wp_postmeta As y_meta
On y_posts.ID = y_meta.post_id
Where y_posts.post_type = 'download'
And y_posts.post_status = 'publish'
And y_meta.meta_key = 'y_activity_locations' 
And y_meta.meta_value Like '%Armidale%'
And y_meta.meta_key = 'y_age'
And y_meta.meta_value Like '%3 to 5%'

該查詢無法正常工作,因為我認為服務器會混淆同一列中的兩個值。

Select y_posts.*, y_meta.*
    From wp_posts As y_posts
    Inner Join wp_postmeta As y_meta
    On y_posts.ID = y_meta.post_id
    Where y_posts.post_type = 'download'
    And y_posts.post_status = 'publish'
    And y_meta.meta_key = 'y_activity_locations' 
    And y_meta.meta_value Like '%Armidale%'
    Or y_meta.meta_key = 'y_age'
    Or y_meta.meta_value Like '%3 to 5%'

此查詢的工作原理,它只給我年齡或位置數據,但我想同時搜索兩個值。

我也嘗試過子查詢(以前從未嘗試過)

Select y_posts.*, y_meta.*
From wp_posts As y_posts
Inner Join wp_postmeta As y_meta
On y_posts.ID = y_meta.post_id
Where y_posts.post_type = 'download'
And y_posts.post_status = 'publish'
And y_meta.meta_key = 'y_activity_locations' 
And y_meta.meta_value Like '%Armidale%'
And (Select yy_meta.* From wp_postmeta As yy_meta Where yy_meta.meta_key = 'y_age'
And yy_meta.meta_value Like '%3 to 5%')

但這給了我這個錯誤

1241-操作數應包含1列

因此,請指導我如何從同一列中獲取兩個值。

請只給我查詢建議而不提供其他解決方案。

請嘗試以下查詢:

    Select y_posts.*, y_meta.*
    From wp_posts As y_posts
    Inner Join wp_postmeta As y_meta
    On y_posts.ID = y_meta.post_id
    Where y_posts.post_type = 'download'
    And y_posts.post_status = 'publish'
    And (
        (y_meta.meta_key = 'y_activity_locations' 
        And y_meta.meta_value Like '%Armidale%')
        Or (y_meta.meta_key = 'y_age'
        And y_meta.meta_value Like '%3 to 5%')
    )

你試試,wordpress查詢

<?php 
$argsCat = array(   
    'posts_per_page'    => -1,              
    'post_type'        => 'download',
    'meta_key'         => 'y_activity_locations',
    'meta_value'       => '%Armidale%',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'y_age',
            'value' => '3',
            'compare' => '>='
        ),
        'relation' => 'AND',
        array(
            'key' => 'y_age',
            'value' => '5',
            'compare' => '<='
        )
    ),

);  
$normal_array = get_posts($argsCat);

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM