仅在 wordpress 中的帖子标题中搜索以获取标题和 ID [英] search only in post titles in the wordpress to get title and id

查看:24
本文介绍了仅在 wordpress 中的帖子标题中搜索以获取标题和 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名安卓开发者.我在我的主机上创建了一个 php 文件,我包含 wp-blog-header.php .我将此文件用作网络服务.

在我的应用程序中,有一个搜索部分,我将 term 作为字符串和 category 作为用户的 id 并将它们发送到我的 php 文件.

现在,我想搜索帖子标题并返回用户想要的标题和ID.

function customSearch($term,$category){...}

我使用准备函数来获取像下面这样的帖子,但我找不到任何只能在帖子标题中搜索的函数.

函数 getLastItems(){$args = 数组('数字帖子' =>5、'偏移' =>0,'类别' =>0,'orderby' =>'发布日期','订单' =>'DESC','post_type' =>'邮政','post_status' =>'发布','suppress_filters' =>真的 );$recent_posts = wp_get_recent_posts( $args, ARRAY_A );$mjson = 数组();foreach( $recent_posts as $recent ){$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($recent['ID']), array(400,300));$url_img = $thumb['0'];$marr = 数组('id'=>$recent["ID"],'title'=>$recent["post_title"],'img'=>$url_img);array_push($mjson,$marr);}返回 $mjson ;}//end 获取最后一项

解决方案

我不明白你的搜索部分,但你的问题只是在标题中搜索,这个答案对你有用.

<块引用>

  1. 在搜索前添加过滤器以仅在标题中搜索.
  2. 搜索后删除过滤器.

如果你想在标题中搜索,只需将下面添加到 args.

function search_by_title( $search, $wp_query ) {if ( !empty( $search ) && !empty( $wp_query->query_vars['search_terms'] ) ) {全球 $wpdb;$q = $wp_query->query_vars;$n = !空($q['精确'])?'' : '%';$search = 数组();foreach ( ( array ) $q['search_terms'] as $term )$search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $n . $wpdb->e​​sc_like( $term ) . $n );如果(!is_user_logged_in())$search[] = "$wpdb->posts.post_password = ''";$search = ' AND ' .内爆( ' AND ', $search );}返回 $search;}$args = 数组('s' =>'搜索字符串','数字帖子' =>5、'偏移' =>0,'类别' =>0,'orderby' =>'发布日期','订单' =>'DESC','post_type' =>'邮政','post_status' =>'发布','suppress_filters' =>真的);add_filter( 'posts_search', 'search_by_title', 10, 2 );$recent_posts = wp_get_recent_posts($args, ARRAY_A);remove_filter('posts_search', 'search_by_title', 500);foreach($recent_posts 作为 $posts) {//您的自定义代码在这里}

I'm an android developer. I made a php file on my host and I include wp-blog-header.php . I used this file as a web service.

In the my app, there is a search part and I get term as String and category as id of user and send them to my php file .

now, I would like to search in post titles and return titles and ids of what user want.

function customSearch($term,$category){
.
.
.

}

I used prepare functions to get posts like below function, but I cannot find any function to search only in post titles.

function getLastItems()
{



    $args = array(
        'numberposts' => 5,
        'offset' => 0,
        'category' => 0,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'post_type' => 'post',
        'post_status' => 'publish',
        'suppress_filters' => true );

    $recent_posts = wp_get_recent_posts( $args, ARRAY_A );

    $mjson = array();


    foreach( $recent_posts as $recent ){

        $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($recent['ID']), array(400,300) );
        $url_img = $thumb['0'];

        $marr = array(
            'id'=>$recent["ID"],
            'title'=>$recent["post_title"],
            'img'=>$url_img
        );

        array_push($mjson,$marr);

    }

    return  $mjson ;



}//end get last items

解决方案

I don't understand your search part, but your question about to search in only title , this answer will do for you.

  1. Add filter to search only in title, before search.
  2. Remove Filter after search.

if you want to search in title, just add below to args.

function search_by_title( $search, $wp_query ) {
    if ( ! empty( $search ) && ! empty( $wp_query->query_vars['search_terms'] ) ) {
        global $wpdb;

        $q = $wp_query->query_vars;
        $n = ! empty( $q['exact'] ) ? '' : '%';

        $search = array();

        foreach ( ( array ) $q['search_terms'] as $term )
            $search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $n . $wpdb->esc_like( $term ) . $n );

        if ( ! is_user_logged_in() )
            $search[] = "$wpdb->posts.post_password = ''";

        $search = ' AND ' . implode( ' AND ', $search );
    }

    return $search;
}


$args = array(
    's' => 'search string',
    'numberposts' => 5,
    'offset' => 0,
    'category' => 0,
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish',
    'suppress_filters' => true);

add_filter( 'posts_search', 'search_by_title', 10, 2 );
$recent_posts = wp_get_recent_posts($args, ARRAY_A);
remove_filter( 'posts_search', 'search_by_title', 500 );


foreach($recent_posts as $posts) {

    // your custom code here
}

这篇关于仅在 wordpress 中的帖子标题中搜索以获取标题和 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆