2017-10-24 2 views
0

私はいくつかのタクソノミーとpost_typesトラフcustom post type UIプラグインを作成しました。 フォームにtaxonomy_idをページに渡し、正しく受信しました。[var_dump($_POST)]は私の例番号30を表示しています。カスタムポストタイプカテゴリの投稿を表示したい:以下のコードを試したが、何も返さなかった。WP_Query cpt taxonomy投稿を読み込んでいません

$args = [ 
    'tax_query' => array(
     array(
      'taxonomy' => 'school_type', 
      'field' => 'term_id', 
      'terms' => array('30','22'), 
      // 'operator' => 'IN' 
     ), 
    'post_type' => 'school', 
    ) 
]; 
if($q->have_posts()): 
    while($q->have_posts()){ 
     $q->the_post(); 
     echo the_title(); 
    } 
else: 
    echo 'nothing'; 
endif; 

誰でも手伝ってもらえますか?

答えて

0

tax_query配列にはpost_typeがあります。また、存在しない場合は$qをターゲティングしています(とにかくわかりません)。あなたはこのような$の引数変数を持っていると、あなたの投稿を取得するためにWP_Query()オブジェクトを使用する必要があります

$args = array(
    'post_type' => 'school', 
    'tax_query'  => array(
     array(
      'taxonomy'  => 'school_type', 
      'field'   => 'term_id', 
      'terms'   => array('30','22'), 
     ), 
    ), 
    'numberposts' => -1 
); 
$q = new WP_Query($args); 
if($q->have_posts()): 
    while($q->have_posts()){ 
     $q->the_post(); 
     echo the_title(); 
    } 
else: 
    echo 'nothing'; 
endif; 
0

- :

はこのような何かを試してみてください。

$args = array(
     'post_type' => 'school', 
     'posts_per_page'=>30, 
     'post_status' => 'publish', 
     'tax_query' => array(array(
      'taxonomy' => 'school_type', 
      'field' => 'term_id', 
      'terms' => array('30','22'), 
    ); 

    $q = new WP_Query($args); 
    if($q->have_posts()): 
      while($q->have_posts()){ 
       $q->the_post(); 
       echo the_title(); 
      } 
     else: 
      echo 'nothing'; 
     endif; 
0

があなたの場合はライン

0

変化型と更新パーマリンクを投稿するには、あなたのパーマリンクの前に$q = new WP_Query($args);を入れて、これが役立つことを願っています。

+0

どうすれば変更できますか? このコードをカスタムページテンプレートに追加しました。 –

+0

設定 - > permalinks-> permalinkを変更して名前を投稿し、permalinkの構造を更新してください。 を追加し、$ q = new WP_Query($ args);を追加します。これはあなたのコードの中で前提条件です。 –

0

私のせいで申し訳ありません。ここに私はこれに変更コードがあり、何も変わっていない。

$args = array(
    'post_type' => 'school', 
    'tax_query'  => array(
     array(
      'taxonomy'  => 'school_type', 
      'field'   => 'term_id', 
      'terms'   => array('30','22'), 
     ), 
    ), 
    'numberposts' => -1 
); 
$q = new WP_Query($args); 
if($q->have_posts()): 
    while($q->have_posts()){ 
     $q->the_post(); 
     echo the_title(); 
    } 
else: 
    echo 'nothing'; 
endif; 
関連する問題