建站教程

建站教程

Products

当前位置:首页 > 建站教程 >

WordPress如何从搜索结果中排除某些帖子或页面(WordPress获取文章标签:get_posts)

GG网络技术分享 2025-03-18 16:13 38


WordPress如何从搜索结果中排除某些帖子或页面

WordPress的搜索结果如果不经过处理,默认情况下是会包含所有类型的文章,不管是page页面还是post文章都会显示,而我们有些时候就需要隐藏某些内容来达到默写目的,这里什么目的就不明说了。

如果想要实现上面的功能,除了自建查询函数之外,还有一种通用的做法如下:

add_action( 'pre_get_posts', 'my_search_exclude_filter' );

function my_search_exclude_filter( $query ) {

if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {

$query->set( 'post__not_in', array( 3, 11, 234, 553 ) );

}

}

代码方面主要用到了pre_get_posts过滤器,而这个过滤器的作用就是在执行wp_query函数之前运行,同时站点之前的文章也介绍过相关函数,但是一笔带过,该过滤器具体描述如下:

pre_get_posts 函数介绍

  • 函数用法
  • do_action_ref_array( 'pre_get_posts', WP_Query $query )Fires after the query variable object is created, but before the actual query is run.
  • 功能介绍
  • Note: If using conditional tags, use the method versions within the passed instance (e.g. $this->is_main_query() instead of is_main_query()). This is because the functions like is_main_query() test against the global $wp_query instance, not the passed one.
  • 参数介绍
  • $query(WP_Query)
    The WP_Query instance (passed by reference).
  • 返回值
  • 暂无数据
  • 如果你不能理解是什么意思,你可以理解为根据指定条件修改查询参数的相关设定,比如说常规查询的主函数显示文章数量是50个,但是你想要在搜索结果页面显示文章数量为20个,那么这样的情况下,不考虑自建查询函数,通过下面的代码可以显示该目的:

    function hwl_home_pagesize( $query ) {

    if ( ! is_admin() && $query->is_main_query() && $query->is_search ) {

    // Display 50 posts for a custom post type called 'movie'

    $query->set( 'posts_per_page', 20 );

    return;

    }

    }

    add_action( 'pre_get_posts', 'hwl_home_pagesize', 1 );

    通过上面的两个例子,如果你了解wp_query主循环的话,那么可以通过这个函数直接写出千奇百怪的情况来实现不同目的,本文标题中还说到了排除页面,言外职业就是仅仅获取文章类型即可(不考虑附件、草稿等情况):

    function search_filter($query) {

    if ( ! is_admin() && $query->is_main_query() ) {

    if ( $query->is_search ) {

    $query->set( 'post_type', 'post' );

    }

    }

    }

    add_action( 'pre_get_posts', 'search_filter' );

    总而言之好好利用这个函数,可以让你的页面千变万化,通过switch丰富展示情况,至于如何展示就看大家的能力了。

    WordPress获取文章标签:get_posts

    WordPress模板标签get_posts用于获取文章信息,通常用于输出最新文章列表、随机文章、指定分类文章等等,在用WordPress制作CMS网站时非常有用。

    get_posts( array $args = null )

    函数参数

    $args

    数组或字符串值

    get_posts()函数$args参数默认的值如下:

    $defaults = array(\\\'numberposts\\\' => 5,\\\'category\\\' => 0,\\\'orderby\\\' => \\\'date\\\',\\\'order\\\' => \\\'DESC\\\',\\\'include\\\' => array(),\\\'exclude\\\' => array(),\\\'meta_key\\\' => \\\'\\\',\\\'meta_value\\\' => \\\'\\\',\\\'post_type\\\' => \\\'post\\\',\\\'suppress_filters\\\' => true);

    get_posts()函数$args参数可用的值如下:

    orderby

    字符串值,默认值:date

    设置排序的类型。

    • none:不使用排序;
    • ID:按ID排序;
    • author:按作者排序;
    • title:按标题排序;
    • date:按日期排序;
    • modified:按文章的修改日期排序;
    • parent:按父级文章ID排序;
    • rand:随机顺序;
    • comment_count:按评论数量排序;
    • menu_order:如果页面,有一个排序设置项,则按这个设置排序;
    • meta_value:按元数据排序;
    • meta_value_num:按元数据排序;
    • post__in:按照传递参数的顺序。

    numberposts

    整数型,默认值:5

    指定输出文章的数量。

    category

    整数型,默认值:0

    指定分类的ID,输出该分类下的文章,默认所有分类。

    order

    字符串值,默认值:DESC

    指定排序的方式。

    • DESC:降序;
    • ASC:升序。

    include

    字符串或数组,默认为空

    指定文章的ID,以输出这些文章的信息。

    exclude

    字符串或数组,默认为空

    指定文章的ID,以排除这些文章。

    meta_key

    字符串值,默认为空

    自定义字段的名称。

    meta_value

    字符串值,默认为空

    自定义字段的值。

    post_type

    字符串值,默认值:post

    文章类型。

    • post:文章;
    • page:页面;
    • revision:文章的修订版本;
    • attachment:附件;
    • 自定义文章类型。

    suppress_filters

    布尔值,默认值:true

    是否使用过滤器。

    函数使用示例

    输出5篇分类1的文章

    <ul><?phpglobal $post;$args = array( \\\'numberposts\\\' => 5, \\\'category\\\' => 1 );$myposts = get_posts( $args );foreach ( $myposts as $post ) : setup_postdata( $post );?><li><a href=\\\"<?php the_permalink(); ?>\\\"><?php the_title(); ?></a></li><?phpendforeach;wp_reset_postdata();?></ul>

    扩展阅读

    get_posts()函数位于:wp-includes/post.php

    相关函数:

    • get_pages()

    标签:

    提交需求或反馈

    Demand feedback