最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - How to search by a post and a category name on wordpress at the same time?

programmeradmin1浏览0评论

I wanna change the default behavior of wordpress search engine. Basically, I want to choose between 2 paths when the user informs a term on the search input:

  • Find the posts or custom posts that have that informed term
  • Or find the posts that belong to a taxonomy (or a custom taxonomy) that has a name like the informed term

So far, I'm trying to use the hook pre_get_posts but I don't know how to combine the 2 paths. I did try something like this:

function change_posts_per_page( $query ) {
    $query->set(
        'tax_query', array(
            array(
                'taxonomy' => 'course-category',
                'field'    => 'name',
                'terms'    => get_query_var('s')
            )
    )); 
}

add_action( 'pre_get_posts', 'change_posts_per_page' );

It doesn't bring the posts with a taxonomy that has the name get_query_var('s'). Can someone help me?

发布评论

评论列表(0)

  1. 暂无评论