Wordpress:'post_name'上的WP_Query搜索条件

Mic*_*umo 7 php wordpress content-management-system

我正在使用WP_Query(非常标准).这一切都很棒.

但是,我做了一个特别的修改,其中,如果用户在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);

?>
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助.如果我能看到如何搜索这个特定的'slug',那就太棒了.

非常感谢,迈克尔.

sxa*_*der 12

代替

'post_name' => 'rebecca-atkinson',
Run Code Online (Sandbox Code Playgroud)

使用:

'name' => 'rebecca-atkinson',
Run Code Online (Sandbox Code Playgroud)


Mic*_*umo 6

除了我在上面的评论中的答案,我还以为我也将其作为正式答案发布:

我必须使用'name'不是 'post_name'.

例如:

'name' => 'rebecca-atkinson' 
Run Code Online (Sandbox Code Playgroud)

希望这有助于将来的某些人.

  • 太烦人了,他们使用“name”而不是“post_name”,这毫无意义,因为其他所有内容都有“post_”前缀!如果您查看文档,会发现 order_by 子句更加令人困惑,它说当按帖子名称排序时,“name”和“post_name”都被接受。 (2认同)