2016-11-15 10 views
0

私はカスタムクエリで検索しているカスタムポストタイプを持っています。あまりにも多くの結果がある場合は、メッセージを出力したい(うまくいく)。しかし、私が持っている問題は、最初にページに行き、検索が実行されていないときです。私はそのメッセージを表示したくありません。検索が実行されていないときに表示を停止するにはどうすればよいですか?任意の検索語が現在のWP_Queryのために送られている場合はtrue検索が実行されたときにのみテキストを出力します

<?php if($the_query->post_count > 20 && is_search()) { ?> 

is_search()リターン:で

<?php if($the_query->post_count > 20) { ?> 

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 

$args = array(
    'posts_per_page' => 5, 
    'post_type'   => 'researchdatabase', 
    'post_status'  => 'publish', 
    'paged'    => $paged 
); 

if($searchTerm != "") { 
    $args['s'] = $searchTerm; 
} 

$the_query = new WP_Query($args); 

if ($the_query->have_posts()) { 
    $counter = 1; ?> 

    <p> 
     <b><?php echo $the_query->post_count; ?> research items</b> 
     <?php if($the_query->post_count > 20) { ?> 
      <br /><span class="research-alert"><b>Refine your search criteria to see fewer results.</b></span> 
     <?php } ?> 
    </p> 
    <hr /> 
    <br /> 

    <?php while ($the_query->have_posts()) { $the_query->the_post(); ?> 

     <?php // output results ?> 

    <?php $counter++; 
    } // end while 

    // reset post data 
    wp_reset_postdata(); 

} else { 
    echo 'No results'; 
} // end if 
?> 
+0

「検索が行われていない場合」とは? –

+0

@RazibAlMamun最初にページに上がったとき、検索フォームに記入されていないときに検索が押されなかったとき。 – Rob

答えて

0

ちょうど条件を変更してください。

+0

それは動作していないようでした、それはテキストを表示しません。それはカスタムポストタイプだと重要ですか? – Rob

+0

それはまだ検索されていません。別の範囲にあるだけです。 'is_search()'の代わりに 'isset($ args ['s'])'を試してみませんか?新しい 'WP_Query'オブジェクトを使用するため、' is_search'は期待通りに動作しません。 – vard

+0

それに運がない、どちらかといえば難しいことだ! – Rob

0

「検索が実行されていません」とは「クエリ内に検索語がありません」という意味です。

if(!empty($searchTerm)) { 
    $args = array(
     's' => $searchTerm 
     'posts_per_page' => 5, 
     'post_type'   => 'researchdatabase', 
     'post_status'  => 'publish', 
     'paged'    => (get_query_var('paged')) ? get_query_var('paged') : 1 
    ); 

    $the_query = new WP_Query($args); 
    // Your code. 
} 

ところで:あなたは、検索結果の総数を表示したい場合、あなたはそれが唯一の最初のページに記事を数える$the_query->found_postsない$the_query->post_countを使用する必要があります。

関連する問題