Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从查询_posts()中排除WordPress自定义分类术语_Php_Wordpress_Query String_Custom Post Type_Custom Taxonomy - Fatal编程技术网

Php 从查询_posts()中排除WordPress自定义分类术语

Php 从查询_posts()中排除WordPress自定义分类术语,php,wordpress,query-string,custom-post-type,custom-taxonomy,Php,Wordpress,Query String,Custom Post Type,Custom Taxonomy,在循环之前,我在我的一个归档页面上添加了以下代码。我需要这个特定的档案以字母顺序显示,而不是按时间顺序。这就是它应该做的 global $query_string; $posts = query_posts($query_string . '&orderby=title&order=asc&posts_per_page=-1'); 我还需要它来排除特定的分类术语。我的分类法称为“公司类型”,我希望它排除术语“特色”。我可以通过添加&company type=featur

在循环之前,我在我的一个归档页面上添加了以下代码。我需要这个特定的档案以字母顺序显示,而不是按时间顺序。这就是它应该做的

global $query_string;
$posts = query_posts($query_string . '&orderby=title&order=asc&posts_per_page=-1');
我还需要它来排除特定的分类术语。我的分类法称为“公司类型”,我希望它排除术语“特色”。我可以通过添加
&company type=featured
,对其进行过滤,以仅显示该分类法术语,但我需要完成相反的操作

我所发现的所有旨在实现这一点的东西都使用了一种非常不同的语法。我试图将我当前的参数与该语法匹配,但没有成功,我无法找出它如何适合这个示例。我看到的示例使用了
tax\u query
参数,但我无法让它与我的代码一起工作


我知道可能有多种方法可以实现这一点,我读到使用
query\u posts
不一定是最好的解决方案,但这是迄今为止我唯一能做到的方法。有人能帮我吗

好的,我让它工作了。我使用了一种不同的语法,我已经尝试了好几次,但它对我不起作用,因为我不知道如何将
$query\u string
中的原始参数包含在其中

有一个名为
wp\u parse\u args
的WordPress函数,可以将它们转换为相同的格式。这就是我最终得到的代码。(我也切换到了
WP\u Query
而不是
Query\u posts
,现在我明白了为什么它以前对我不起作用了。)


看看这个。这会对你有帮助的。谢谢,@Gunaseelan。我在最初的搜索中确实看到了这篇文章,我试图用这种格式实现我的其余代码,但我无法让它工作。很好,你切换到了
WP\u Query
Query\u posts()
是执行自定义查询的一种可怕的方式,永远不应该使用
global $query_string;

$args = array( 
  'tax_query' =>  array (
    array(
      'taxonomy' => 'company-type', // My Custom Taxonomy
      'terms' => 'featured', // My Taxonomy Term that I wanted to exclude
      'field' => 'slug', // Whether I am passing term Slug or term ID
      'operator' => 'NOT IN', // Selection operator - use IN to include, NOT IN to exclude
    ),
  ),
  'posts_per_page' => -1,
  'orderby' => 'title',
  'order'=>'ASC'
);

$args = wp_parse_args( $query_string, $args );
$query = new WP_Query( $args );