2011-09-29 64 views
4

我正在使用WP_Query(漂亮的標準)。這一切都很好。WordPress的WP_Query搜索條件'post_name'

但是,我做了一個特殊的修改,如果用戶在URL中輸入特定的帖子名稱,搜索將只返回與該post_name值匹配的帖子。

看到我的代碼在下面評論有關特定行不起作用。

<?php 

$getPeople = array(
    'post_type' => 'person', 
    'posts_per_page' => -1, 
    // I want this below to only return me the post with this specific value. 
    // This doesn't error, but doesn't work either. 
    // I know it seems counter-productive to a 'search' but this particular case requires it. 
    // This has a hard-coded value at the moment. 
    'post_name' => 'rebecca-atkinson', 
    'orderby' => 'meta_value', 
    'meta_key' => 'last-name', 
    'order' => 'ASC', 

    'meta_query' => array(
     array(
      'key' => 'gender', 
      'value' => $theGender, 
     ) 
    ), 

    'tax_query' => array(

     'relation' => 'OR', 

     array(
      'taxonomy' => 'accent', 
      'field' => 'slug', 
      'terms' => $theAccent, 
      'operator' => 'IN', 
     ), 
     array(
      'taxonomy' => 'style', 
      'field' => 'slug', 
      'terms' => $theStyle, 
      'operator' => 'IN', 
     ), 
     array(
      'taxonomy' => 'age', 
      'field' => 'slug', 
      'terms' => $theAge, 
      'operator' => 'IN', 
     ), 

    ) 
); 

$myposts = new WP_Query($getPeople); 

?> 

您的幫助將不勝感激。如果我只能看到如何搜索這個特定的'slu'',那就太好了。

非常感謝, Michael。

+2

沒關係,我想通了!我必須使用'name'而不是'post_name'。 'name'=>'rebecca-atkinson', –

+0

http://wordpress.stackexchange.com/a/18715/22616 – softsdev

回答

4

除了我的上述評論的答案,我想我會發布它作爲一個正式的答案太:

我必須使用'name'而不是'post_name'

例如:

'name' => 'rebecca-atkinson' 

希望這可以幫助別人的未來。

10

而不是

'post_name' => 'rebecca-atkinson', 

使用:

'name' => 'rebecca-atkinson', 
+0

不錯..我可以通過這樣做使其工作..但是,是否有任何解釋那? – Leonardo

+0

檢查WP_Query文檔。 'post_name'不是有效的查詢參數。請參閱:https://codex.wordpress.org/Class_Reference/WP_Query – sxalexander