programing

워드 프레스 쿼리를 사용하여 끈적거리는 게시물만 표시

mailnote 2023. 9. 13. 22:53
반응형

워드 프레스 쿼리를 사용하여 끈적거리는 게시물만 표시

다음은 끈적끈적한 게시물만 보여주고 싶지만 쿼리에 게시물이 표시되지 않는 제 워드프레스 쿼리입니다.그리고 그 부분을 확인할 수 있도록 두 개의 게시물을 끈적하게 설정했습니다!!!이 쿼리를 수정하는 방법을 알려주세요. 그러면 끈적거리는 게시물만 표시됩니다.

<?php 
   $wp_query = null; 
  $wp_query =  new WP_Query(array(
 'posts_per_page' => 2,
 //'paged' => get_query_var('paged'),
 'post_type' => 'post',
'post__in'  =>  'sticky_posts',
 //'post__not_in' => array($lastpost),
 'post_status' => 'publish',
 'caller_get_posts'=> 0 ));

  while ($wp_query->have_posts()) : $wp_query->the_post(); $lastpost[] = get_the_ID();
?>

끈적끈적한 게시물만 표시하는 쿼리:

// get sticky posts from DB
$sticky = get_option('sticky_posts');
// check if there are any
if (!empty($sticky)) {
    // optional: sort the newest IDs first
    rsort($sticky);
    // override the query
    $args = array(
        'post__in' => $sticky
    );
    query_posts($args);
    // the loop
    while (have_posts()) {
         the_post();
         // your code
    }
}

query_posts() 함수는 현재 쿼리를 설정하기 전에 새 WP_Query()를 생성합니다. 이는 최적의 효율적인 메서드가 아니며 추가 SQL 요청을 수행합니다.

안전을 위해 'pre_get_posts' 후크를 사용합니다.

function sticky_home( $query ) {

    $sticky = get_option('sticky_posts');

    if (! empty($sticky)) {
        if ( $query->is_home() && $query->is_main_query() ) {
             $query->set( 'post__in', $sticky );
        }
    }

} add_action( 'pre_get_posts', 'sticky_home' );

언급URL : https://stackoverflow.com/questions/19814320/wordpress-query-to-show-only-sticky-posts

반응형