如何使用WP_QUERY搜索“帖子标题”和“标签”?

时间:2019-11-12 作者:Elle

在我的自定义搜索表单中,我当前在WP查询中使用以下类型的$args:

$args = array(

    \'post_type\'   => \'post\',
    \'posts_per_page\'  => $postsPerPage,
    \'offset\'          => $postOffset,
    \'category\'        => 42,
    \'s\'               => $name, //value of my searched term
    \'orderby\'         => \'date\',
    \'order\'           => \'DESC\',

);
我想按“标题”或“标签”搜索帖子。

我已尝试使用此代码:

$query_args = array(

\'post_type\' => \'post\',

\'post_status\' => \'publish\',

\'order\' => \'ASC\',

\'orderby\' => \'none\',

\'posts_per_page\' => \'100\',

\'offset\' => \'2\',

\'page\' => \'2\',

\'tax_query\' => array(

    \'0\' => array(

        \'taxonomy\' => \'post_tag\',

        \'field\' => \'name\',

        \'terms\' => array(\'test\'),

        \'operator\' => \'IN\',

    ),

    \'relation\' => \'OR\',

),

\'meta_query\' => array(

    \'0\' => array(

        \'key\' => \'post_title\',

        \'value\' => \'test\',

        \'compare\' => \'LIKE\',

    ),

    \'relation\' => \'OR\',

),
但结果是空的。

谢谢

1 个回复
SO网友:Dino Morrison

尝试删除tax\\u查询和meta\\u查询数组中的“0”键。

$query_args = array(

    \'post_type\' => \'post\',

    \'post_status\' => \'publish\',

    \'order\' => \'ASC\',

    \'orderby\' => \'none\',

    \'posts_per_page\' => \'100\',

    \'offset\' => \'2\',

    \'page\' => \'2\',

    \'tax_query\' => array(

        array(

            \'taxonomy\' => \'post_tag\',

            \'field\' => \'name\',

            \'terms\' => array(\'test\'),

            \'operator\' => \'IN\',

        ),

        \'relation\' => \'OR\',

    ),

    \'meta_query\' => array(

        array(

            \'key\' => \'post_title\',

            \'value\' => \'test\',

            \'compare\' => \'LIKE\',

        ),

        \'relation\' => \'OR\',

    ),
在您的第一组参数中\'category\' => 42, 但这应该是\'cat\' => 42,

相关推荐

清理Get_Query_var()URL参数

我目前正在一个网站上工作,并测试其安全性。其中一个页面具有排序功能,我在其中传递一个url参数,说明我希望如何对内容进行排序。例如:www.example.com/page/?sort=alpha 这很好,但我也尝试发送恶意代码:www.example.com/page/?sort=alpha%3Cimg+src=xyz+onerror=alert(99)%3E%3Cxss/%3E 在internet explorer中,当我输入此url时,我的页面会显示出来,并弹出一个javascr