META_QUERY,其中值等于给定值

时间:2016-09-22 作者:Thomas_Hoadley

我目前有一个自定义查询,我正在尝试按艺术家筛选产品(绘画)。

                    $artistID = $_GET[\'artist_id\'];

                    $args = array(
                      \'post_type\' => \'product\',
                      \'meta_query\'     => array(
                        array(
                          \'key\'     => \'artist\',
                          \'value\'   => $artistID
                        ) 
                      )
                    );

                $loop = new WP_Query( $args );
它目前不起作用。。。

当我跑步时

var_dump(get_post_meta(get_the_ID()));
它输出以下内容(对艺术家键感兴趣)。。。有人能帮我写一个查询,只获取具有给定值的帖子吗?值得注意的是,post的meta\\u数据来自作为post对象的高级自定义字段关系字段。

array(41) {
      ["_edit_lock"]=>
      array(1) {
        [0]=>
        string(12) "1473170740:7"
      }
      ["_edit_last"]=>
      array(1) {
        [0]=>
        string(1) "7"
      }
      ["_thumbnail_id"]=>
      array(1) {
        [0]=>
        string(3) "279"
      }
      ["_product_attributes"]=>
      array(1) {
        [0]=>
        string(510) "a:3:{s:12:"pa_hang-type";a:6:{s:4:"name";s:12:"pa_hang-type";s:5:"value";s:0:"";s:8:"position";s:1:"0";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:18:"pa_limited-edition";a:6:{s:4:"name";s:18:"pa_limited-edition";s:5:"value";s:0:"";s:8:"position";s:1:"1";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:7:"pa_size";a:6:{s:4:"name";s:7:"pa_size";s:5:"value";s:0:"";s:8:"position";s:1:"3";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}}"
      }
      ["_visibility"]=>
      array(1) {
        [0]=>
        string(7) "visible"
      }
      ["_stock_status"]=>
      array(1) {
        [0]=>
        string(7) "instock"
      }
      ["total_sales"]=>
      array(1) {
        [0]=>
        string(1) "0"
      }
      ["_downloadable"]=>
      array(1) {
        [0]=>
        string(2) "no"
      }
      ["_virtual"]=>
      array(1) {
        [0]=>
        string(2) "no"
      }
      ["_tax_status"]=>
      array(1) {
        [0]=>
        string(7) "taxable"
      }
      ["_tax_class"]=>
      array(1) {
        [0]=>
        string(0) ""
      }
      ["_purchase_note"]=>
      array(1) {
        [0]=>
        string(0) ""
      }
      ["_featured"]=>
      array(1) {
        [0]=>
        string(2) "no"
      }
      ["_weight"]=>
      array(1) {
        [0]=>
        string(0) ""
      }
      ["_length"]=>
      array(1) {
        [0]=>
        string(0) ""
      }
      ["_width"]=>
      array(1) {
        [0]=>
        string(0) ""
      }
      ["_height"]=>
      array(1) {
        [0]=>
        string(0) ""
      }
      ["_sku"]=>
      array(1) {
        [0]=>
        string(0) ""
      }
      ["_regular_price"]=>
      array(1) {
        [0]=>
        string(2) "60"
      }
      ["_sale_price"]=>
      array(1) {
        [0]=>
        string(0) ""
      }
      ["_sale_price_dates_from"]=>
      array(1) {
        [0]=>
        string(0) ""
      }
      ["_sale_price_dates_to"]=>
      array(1) {
        [0]=>
        string(0) ""
      }
      ["_price"]=>
      array(1) {
        [0]=>
        string(2) "60"
      }
      ["_sold_individually"]=>
      array(1) {
        [0]=>
        string(0) ""
      }
      ["_manage_stock"]=>
      array(1) {
        [0]=>
        string(2) "no"
      }
      ["_backorders"]=>
      array(1) {
        [0]=>
        string(2) "no"
      }
      ["_stock"]=>
      array(1) {
        [0]=>
        string(0) ""
      }
      ["_upsell_ids"]=>
      array(1) {
        [0]=>
        string(6) "a:0:{}"
      }
      ["_crosssell_ids"]=>
      array(1) {
        [0]=>
        string(6) "a:0:{}"
      }
      ["_product_version"]=>
      array(1) {
        [0]=>
        string(5) "2.6.4"
      }
      ["_product_image_gallery"]=>
      array(1) {
        [0]=>
        string(3) "279"
      }
      ["artist"]=>
      array(1) {
        [0]=>
        string(3) "168"
      }
      ["_artist"]=>
      array(1) {
        [0]=>
        string(19) "field_57c6d280c7a91"
      }
      ["_yoast_wpseo_primary_product_cat"]=>
      array(1) {
        [0]=>
        string(2) "32"
      }
      ["_yoast_wpseo_focuskw_text_input"]=>
      array(1) {
        [0]=>
        string(23) "Worker Bee Wildlife Art"
      }
      ["_yoast_wpseo_focuskw"]=>
      array(1) {
        [0]=>
        string(23) "Worker Bee Wildlife Art"
      }
      ["_yoast_wpseo_linkdex"]=>
      array(1) {
        [0]=>
        string(2) "22"
      }
      ["_yoast_wpseo_content_score"]=>
      array(1) {
        [0]=>
        string(2) "30"
      }
      ["_wc_rating_count"]=>
      array(1) {
        [0]=>
        string(6) "a:0:{}"
      }
      ["_wc_average_rating"]=>
      array(1) {
        [0]=>
        string(1) "0"
      }
      ["_a3_dgallery"]=>
      array(1) {
        [0]=>
        string(3) "279"
      }
    }
还请参见var\\u dump($loop)输出。

                object(WP_Query)#8961 (49) {
  ["query"]=>
  array(2) {
    ["post_type"]=>
    string(7) "product"
    ["meta_query"]=>
    array(1) {
      [0]=>
      array(2) {
        ["key"]=>
        string(6) "artist"
        ["value"]=>
        string(3) "174"
      }
    }
  }
  ["query_vars"]=>
  array(65) {
    ["post_type"]=>
    string(7) "product"
    ["meta_query"]=>
    array(1) {
      [0]=>
      array(2) {
        ["key"]=>
        string(6) "artist"
        ["value"]=>
        string(3) "174"
      }
    }
    ["error"]=>
    string(0) ""
    ["m"]=>
    string(0) ""
    ["p"]=>
    int(0)
    ["post_parent"]=>
    string(0) ""
    ["subpost"]=>
    string(0) ""
    ["subpost_id"]=>
    string(0) ""
    ["attachment"]=>
    string(0) ""
    ["attachment_id"]=>
    int(0)
    ["name"]=>
    string(0) ""
    ["static"]=>
    string(0) ""
    ["pagename"]=>
    string(0) ""
    ["page_id"]=>
    int(0)
    ["second"]=>
    string(0) ""
    ["minute"]=>
    string(0) ""
    ["hour"]=>
    string(0) ""
    ["day"]=>
    int(0)
    ["monthnum"]=>
    int(0)
    ["year"]=>
    int(0)
    ["w"]=>
    int(0)
    ["category_name"]=>
    string(0) ""
    ["tag"]=>
    string(0) ""
    ["cat"]=>
    string(0) ""
    ["tag_id"]=>
    string(0) ""
    ["author"]=>
    string(0) ""
    ["author_name"]=>
    string(0) ""
    ["feed"]=>
    string(0) ""
    ["tb"]=>
    string(0) ""
    ["paged"]=>
    int(0)
    ["meta_key"]=>
    string(0) ""
    ["meta_value"]=>
    string(0) ""
    ["preview"]=>
    string(0) ""
    ["s"]=>
    string(0) ""
    ["sentence"]=>
    string(0) ""
    ["title"]=>
    string(0) ""
    ["fields"]=>
    string(0) ""
    ["menu_order"]=>
    string(0) ""
    ["embed"]=>
    string(0) ""
    ["category__in"]=>
    array(0) {
    }
    ["category__not_in"]=>
    array(0) {
    }
    ["category__and"]=>
    array(0) {
    }
    ["post__in"]=>
    array(0) {
    }
    ["post__not_in"]=>
    array(0) {
    }
    ["post_name__in"]=>
    array(0) {
    }
    ["tag__in"]=>
    array(0) {
    }
    ["tag__not_in"]=>
    array(0) {
    }
    ["tag__and"]=>
    array(0) {
    }
    ["tag_slug__in"]=>
    array(0) {
    }
    ["tag_slug__and"]=>
    array(0) {
    }
    ["post_parent__in"]=>
    array(0) {
    }
    ["post_parent__not_in"]=>
    array(0) {
    }
    ["author__in"]=>
    array(0) {
    }
    ["author__not_in"]=>
    array(0) {
    }
    ["ignore_sticky_posts"]=>
    bool(false)
    ["suppress_filters"]=>
    bool(false)
    ["cache_results"]=>
    bool(true)
    ["update_post_term_cache"]=>
    bool(true)
    ["lazy_load_term_meta"]=>
    bool(true)
    ["update_post_meta_cache"]=>
    bool(true)
    ["posts_per_page"]=>
    int(8)
    ["nopaging"]=>
    bool(false)
    ["comments_per_page"]=>
    string(2) "50"
    ["no_found_rows"]=>
    bool(false)
    ["order"]=>
    string(4) "DESC"
  }
  ["tax_query"]=>
  object(WP_Tax_Query)#8958 (6) {
    ["queries"]=>
    array(0) {
    }
    ["relation"]=>
    string(3) "AND"
    ["table_aliases":protected]=>
    array(0) {
    }
    ["queried_terms"]=>
    array(0) {
    }
    ["primary_table"]=>
    string(10) "wp_2_posts"
    ["primary_id_column"]=>
    string(2) "ID"
  }
  ["meta_query"]=>
  object(WP_Meta_Query)#8959 (9) {
    ["queries"]=>
    array(2) {
      [0]=>
      array(2) {
        ["key"]=>
        string(6) "artist"
        ["value"]=>
        string(3) "174"
      }
      ["relation"]=>
      string(2) "OR"
    }
    ["relation"]=>
    string(3) "AND"
    ["meta_table"]=>
    string(13) "wp_2_postmeta"
    ["meta_id_column"]=>
    string(7) "post_id"
    ["primary_table"]=>
    string(10) "wp_2_posts"
    ["primary_id_column"]=>
    string(2) "ID"
    ["table_aliases":protected]=>
    array(1) {
      [0]=>
      string(13) "wp_2_postmeta"
    }
    ["clauses":protected]=>
    array(1) {
      ["wp_2_postmeta"]=>
      array(5) {
        ["key"]=>
        string(6) "artist"
        ["value"]=>
        string(3) "174"
        ["compare"]=>
        string(1) "="
        ["alias"]=>
        string(13) "wp_2_postmeta"
        ["cast"]=>
        string(4) "CHAR"
      }
    }
    ["has_or_relation":protected]=>
    bool(false)
  }
  ["date_query"]=>
  bool(false)
  ["request"]=>
  string(453) "SELECT SQL_CALC_FOUND_ROWS  wp_2_posts.ID FROM wp_2_posts  INNER JOIN wp_2_postmeta ON ( wp_2_posts.ID = wp_2_postmeta.post_id ) WHERE 1=1  AND ( 
  ( wp_2_postmeta.meta_key = \'artist\' AND wp_2_postmeta.meta_value = \'174\' )
) AND wp_2_posts.post_type = \'product\' AND (wp_2_posts.post_status = \'publish\' OR wp_2_posts.post_status = \'acf-disabled\' OR wp_2_posts.post_status = \'private\') GROUP BY wp_2_posts.ID ORDER BY wp_2_posts.post_date DESC LIMIT 0, 8"
  ["posts"]=>
  array(6) {
    [0]=>
    object(WP_Post)#8957 (24) {
      ["ID"]=>
      int(297)
      ["post_author"]=>
      string(1) "7"
      ["post_date"]=>
      string(19) "2016-09-07 15:23:42"
      ["post_date_gmt"]=>
      string(19) "2016-09-07 14:23:42"
      ["post_content"]=>
      string(0) ""
      ["post_title"]=>
      string(5) "Alone"
      ["post_excerpt"]=>
      string(0) ""
      ["post_status"]=>
      string(7) "publish"
      ["comment_status"]=>
      string(4) "open"
      ["ping_status"]=>
      string(6) "closed"
      ["post_password"]=>
      string(0) ""
      ["post_name"]=>
      string(5) "alone"
      ["to_ping"]=>
      string(0) ""
      ["pinged"]=>
      string(0) ""
      ["post_modified"]=>
      string(19) "2016-09-07 15:23:42"
      ["post_modified_gmt"]=>
      string(19) "2016-09-07 14:23:42"
      ["post_content_filtered"]=>
      string(0) ""
      ["post_parent"]=>
      int(0)
      ["guid"]=>
      string(55) "http://shop.localhost.com/?post_type=product&p=297"
      ["menu_order"]=>
      int(0)
      ["post_type"]=>
      string(7) "product"
      ["post_mime_type"]=>
      string(0) ""
      ["comment_count"]=>
      string(1) "0"
      ["filter"]=>
      string(3) "raw"
    }
    [1]=>
    object(WP_Post)#8956 (24) {
      ["ID"]=>
      int(295)
      ["post_author"]=>
      string(1) "7"
      ["post_date"]=>
      string(19) "2016-09-07 09:36:34"
      ["post_date_gmt"]=>
      string(19) "2016-09-07 08:36:34"
      ["post_content"]=>
      string(0) ""
      ["post_title"]=>
      string(11) "Reef Glider"
      ["post_excerpt"]=>
      string(69) "This is a description of the Product. It does not need to be entered."
      ["post_status"]=>
      string(7) "publish"
      ["comment_status"]=>
      string(4) "open"
      ["ping_status"]=>
      string(6) "closed"
      ["post_password"]=>
      string(0) ""
      ["post_name"]=>
      string(11) "reef-glider"
      ["to_ping"]=>
      string(0) ""
      ["pinged"]=>
      string(0) ""
      ["post_modified"]=>
      string(19) "2016-09-19 12:06:24"
      ["post_modified_gmt"]=>
      string(19) "2016-09-19 11:06:24"
      ["post_content_filtered"]=>
      string(0) ""
      ["post_parent"]=>
      int(0)
      ["guid"]=>
      string(55) "http://shop.localhost.com/?post_type=product&p=295"
      ["menu_order"]=>
      int(0)
      ["post_type"]=>
      string(7) "product"
      ["post_mime_type"]=>
      string(0) ""
      ["comment_count"]=>
      string(1) "0"
      ["filter"]=>
      string(3) "raw"
    }
    [2]=>
    object(WP_Post)#8955 (24) {
      ["ID"]=>
      int(293)
      ["post_author"]=>
      string(1) "7"
      ["post_date"]=>
      string(19) "2016-09-07 09:33:30"
      ["post_date_gmt"]=>
      string(19) "2016-09-07 08:33:30"
      ["post_content"]=>
      string(0) ""
      ["post_title"]=>
      string(16) "Heart of The Sea"
      ["post_excerpt"]=>
      string(0) ""
      ["post_status"]=>
      string(7) "publish"
      ["comment_status"]=>
      string(4) "open"
      ["ping_status"]=>
      string(6) "closed"
      ["post_password"]=>
      string(0) ""
      ["post_name"]=>
      string(16) "heart-of-the-sea"
      ["to_ping"]=>
      string(0) ""
      ["pinged"]=>
      string(0) ""
      ["post_modified"]=>
      string(19) "2016-09-19 16:48:53"
      ["post_modified_gmt"]=>
      string(19) "2016-09-19 15:48:53"
      ["post_content_filtered"]=>
      string(0) ""
      ["post_parent"]=>
      int(0)
      ["guid"]=>
      string(55) "http://shop.localhost.com/?post_type=product&p=293"
      ["menu_order"]=>
      int(0)
      ["post_type"]=>
      string(7) "product"
      ["post_mime_type"]=>
      string(0) ""
      ["comment_count"]=>
      string(1) "0"
      ["filter"]=>
      string(3) "raw"
    }
    [3]=>
    object(WP_Post)#8954 (24) {
      ["ID"]=>
      int(291)
      ["post_author"]=>
      string(1) "7"
      ["post_date"]=>
      string(19) "2016-09-06 16:46:38"
      ["post_date_gmt"]=>
      string(19) "2016-09-06 15:46:38"
      ["post_content"]=>
      string(0) ""
      ["post_title"]=>
      string(6) "Escape"
      ["post_excerpt"]=>
      string(0) ""
      ["post_status"]=>
      string(7) "publish"
      ["comment_status"]=>
      string(4) "open"
      ["ping_status"]=>
      string(6) "closed"
      ["post_password"]=>
      string(0) ""
      ["post_name"]=>
      string(6) "escape"
      ["to_ping"]=>
      string(0) ""
      ["pinged"]=>
      string(0) ""
      ["post_modified"]=>
      string(19) "2016-09-06 16:46:38"
      ["post_modified_gmt"]=>
      string(19) "2016-09-06 15:46:38"
      ["post_content_filtered"]=>
      string(0) ""
      ["post_parent"]=>
      int(0)
      ["guid"]=>
      string(55) "http://shop.localhost.com/?post_type=product&p=291"
      ["menu_order"]=>
      int(0)
      ["post_type"]=>
      string(7) "product"
      ["post_mime_type"]=>
      string(0) ""
      ["comment_count"]=>
      string(1) "0"
      ["filter"]=>
      string(3) "raw"
    }
    [4]=>
    object(WP_Post)#8772 (24) {
      ["ID"]=>
      int(289)
      ["post_author"]=>
      string(1) "7"
      ["post_date"]=>
      string(19) "2016-09-06 16:02:57"
      ["post_date_gmt"]=>
      s…

2 个回复
SO网友:FaCE

就快到了--只需将比较添加到元查询中:

$args = array(
  \'post_type\' => \'product\',
  \'meta_query\'     => array(
    array(
      \'key\'     => \'artist\',
      \'value\'   => $artistID,
      \'compare\' => \'=\'
    ) 
  )
);

https://codex.wordpress.org/Class_Reference/WP_Meta_Query

记住过滤$artistID 在您的查询中接受它之前!

SO网友:Amine Faiz

首先,出于安全性考虑,您需要在接受Artister\\u id变量之前对其进行筛选,其次,您应该在查询循环中运行var\\u dump(get\\u post\\u meta(get\\u the\\u id())),尝试以下代码,它应该适合您:

<?php 
    $artistID = filter_input(INPUT_GET, \'artist_id\', FILTER_SANITIZE_NUMBER_INT);
    $args = array(
        //Type & Status Parameters
        \'post_type\'   => \'product\',
        //Order & Orderby Parameters
        \'order\'               => \'DESC\',
        \'orderby\'             => \'date\',

        //Pagination Parameters
        \'posts_per_page\'         => -1,
        //Custom Field Parameters
        \'meta_key\'       => \'artist\',
        \'meta_value\'     => $artistID,
        \'meta_compare\'   => \'=\',

    );

$query = new WP_Query( $args );

if ($query->have_posts()) {
    while($query->have_posts()) {
        $query->the_post();
        global $post;
        var_dump(get_post_meta($post->ID);
    }
}
?>

相关推荐

Trim posts from WP-Query?

我希望在我的首页上显示一些由以下标准确定的帖子:显示当月的所有帖子(实际上是从最近帖子对应的月份开始);但是如果这个数字少于(比如)8,那么就显示最近的8篇帖子。假设我知道每个月都不会有超过(比如)20篇文章,我会将posts\\u per\\u page设置为20,通常抓取最近的20篇文章,然后在php中进行适当的修剪(切片)。(在SQL中这样做似乎太复杂了)顺便说一句:起初我以为add_filter( \'posts_results\', ... ) 这很合适,但很难区分查询是与主循环相对应,还是与其