워드프레스 검색 기능으로 게시물만 검색
워드프레스 검색 기능을 사용하고 싶은데 블로그 게시물만 검색하고 정적 페이지는 쿼리에서 제외했으면 합니다.이거 어떻게 하는 거야?저는 플러그인 사용에 반대하지 않습니다.
정답은 여기 있습니다
http://www.shilling.id.au/2011/06/23/wordpress-search-only-show-post-and-not-pages/
<?php
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
?>
간단하게 추가<input type="hidden" name="post_type" value="post" />
테마 검색 양식에...안부 전해 주세요!
이 솔루션은 다른 게시 유형을 지정하지 않은 경우 검색에서 게시물만 검색합니다.이 방법은 다른 검색 필드의 숨겨진 필드에 사용자 지정 게시 유형을 지정하는 경우 간섭하지 않습니다.
function searchFilter($query) {
if ($query->is_search) {
if ( !isset($query->query_vars['post_type']) ) {
$query->set('post_type', 'post');
}
}
return $query;
}
add_filter('pre_get_posts','searchFilter');
이 wp 포럼 페이지에는 사이트를 편집하는 위치에 따라 다양한 방법의 예가 있습니다(index.php 또는 wp-includes/query.php는 옵션이라고 생각합니다).
http://wordpress.org/support/topic/possible-search-only-posts-exclude-pages
대략적인 해결방법은 빈약하다.핵심 편집은 WordPress 설치의 업그레이드 가능성에 방해가 됩니다. 업데이트 시마다 이 변경을 반복해야 합니다.다른 솔루션은 검색 후 결과를 필터링하여 데이터베이스에 불필요한 부하를 줍니다.더 나은 솔루션:
테마의 functions.php에 검색 폼을 작성하는 새로운 함수를 추가합니다.
function custom_search_form( $form, $value = "Search", $post_type = 'post' ) {
$form_value = (isset($value)) ? $value : attribute_escape(apply_filters('the_search_query', get_search_query()));
$form = '<form method="get" id="searchform" action="' . get_option('home') . '/" >
<div>
<input type="hidden" name="post_type" value="'.$post_type.'" />
<input type="text" value="' . $form_value . '" name="s" id="s" />
<input type="submit" id="searchsubmit" value="'.attribute_escape(__('Search')).'" />
</div>
</form>';
return $form;
}
이제 양식을 원하는 템플리트(또는 작성한 위젯 내에서 위젯으로 쉽게 등록할 수 있음)에서 다음과 같이 됩니다.
<?= custom_search_form( null, 'Search posts', 'post'); ?>
인수들은 함수와 호출에서 빠질 수 있지만, 저는 그것들이 도움이 된다고 생각합니다.이 모든 것의 열쇠는 숨겨진 입력 'post_type'입니다.이 입력은 값을 쿼리에 전달합니다.디폴트로는post
는, 투고만이 반환되는 것을 보증합니다.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php if (is_search() && ($post->post_type=='page')) continue; ?>
이거 한번 써보고 효과가 있는지 알려주세요.
쿼리를 글로벌 쿼리와 병합합니다.
global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => 'post' ) );
query_posts( $args );
WP Search http://wpsear.ch/ 를 사용하여 결과에 표시할 게시 유형을 구성할 수 있습니다.
'<form role="search" method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '">
<div>
<label class="screen-reader-text" for="s">' . _x( 'Search for:', 'label' ) . '</label>
<input type="text" value="' . get_search_query() . '" name="s" id="s" />
<input type="submit" id="searchsubmit" value="'. esc_attr_x( 'Search', 'submit button' ) .'" />
</div>'
</form>;
여러 검색 폼을 사용하는 경우 다음 코드는 특정 폼을 다음과 같이 제한하는 데 유용합니다.post_type
포스트.
/**
* Modify search query
*
* @param WP_Query $query
*
* @return void
*/
function theme_modify_search( $query ) {
if( is_admin() ) {
return;
}
if( $query->is_main_query() ) {
if( isset( $_GET, $_GET['post_type'] ) ) {
$query->set( 'post_type', $_GET['post_type'] );
}
}
}
add_action( 'pre_get_posts', 'theme_modify_search' );
상세한 것에 대하여는, https://wordpress.org/support/topic/limit-a-specific-search-form-to-post-only/ 를 참조해 주세요.
언급URL : https://stackoverflow.com/questions/4198842/wordpress-search-function-to-only-search-posts
'programing' 카테고리의 다른 글
Python의 문자열에서 사전으로 (0) | 2023.03.31 |
---|---|
JSONP는 안전한가요? (0) | 2023.03.31 |
필수 필드의 ngMessage를 더티 또는 폼 전송 시에만 표시하려면 어떻게 해야 합니까? (0) | 2023.03.31 |
각도 응용 프로그램 시작 시 코드 실행 (0) | 2023.03.15 |
Spring Security에서 권장되지 않는 Authorization Server를 대체할 수 있는 것은 무엇입니까? (0) | 2023.03.15 |