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
'programing' 카테고리의 다른 글
MYSQL로 연결된 동적 PHP 사이트를 CMS로 변환하는 쉬운 방법? (0) | 2023.10.11 |
---|---|
CSS 애니메이션 종료 시 최종 상태 유지 (0) | 2023.10.11 |
다른 파이프라인 함수를 호출하는 파이프라인 함수 (0) | 2023.10.11 |
구분 기호를 사용한 MySQL 부분 문자열 추출 (0) | 2023.10.11 |
Mysql 색상 체계 (0) | 2023.10.11 |