programing

WordPress get_posts를 제목별로 예를 들어 다음과 같이 입력합니다.

javajsp 2023. 10. 11. 20:30

WordPress get_posts를 제목별로 예를 들어 다음과 같이 입력합니다.

워드프레스의 작은 검색기능을 만들려고 합니다.AJAX 콜은 제목이 다음과 같은 모든 게시물을 받아야 합니다.%quote%.

이런 일이 일어날 가능성이 있나요?get_posts()기능?

오해하지 마세요.아약스는 잘 작동합니다.제 기능에는 아약스 기능이 있습니다.php와 저는 게시물을 받습니다.제가 해결책을 찾을 수 없었던 "where title like" 부분일 뿐입니다.

사용자 지정 검색 쿼리를 수행할 수도 있습니다.

$search_query = "SELECT ID FROM {$wpdb->prefix}posts
                         WHERE post_type = 'post' 
                         AND post_title LIKE %s";
    
$like = '%' . $quote . '%';
$results = $wpdb->get_results($wpdb->prepare($search_query, $like), ARRAY_A);

$quote_ids = array_column($results, 'ID');

$quotes = get_posts(array('post_type'=>'post', 'orderby'=>'title', 'order'=>'ASC', 'post__in' => $quote_ids));

아니요, 하지만 사용자 정의 루프를 만들 수 있습니다.

이거 봐요.

편집:

$args = array('s' => 'keyword');

$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        //whatever you want to do with each post
    }
} else {
     // no posts found
}   

아니면 필터를 이용해서posts_where다음과 같이:

$options = array(
    'posts_per_page' => -1,
    'suppress_filters' => false, // important!
    'post_type' => 'post',
    'post_status' => 'publish',
);
$keyword = 'quote';

add_filter( 'posts_where', 'my_filter_post_where' );
$posts = get_posts( $options );
remove_filter( 'posts_where', 'my_filter_post_where' );

function my_filter_post_where( $where) {
    global $wpdb;
    global $keyword;

    $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $keyword ) ) . '%\'';

    return $where;
}

언급URL : https://stackoverflow.com/questions/25103949/wordpress-get-posts-by-title-like