2016-04-07 6 views
0

私はカスタムポストタイプ 'cars'を持っていて、その子ポストタイプは 'carvariants'です。私が何をしたいか現在のカスタム投稿の子投稿を取得し、カスタムフィールド番号で注文するにはどうすればいいですか?

が取得され、子ポスト(carvariants)現在のポスト(車)。私はこのコードを試してみました:

<div> 
    <?php 
    $parent_id = 1064; 
    $the_query = new WP_Query(array(
'post_parent' => $parent_id, 
     'post_type'   => 'carvariants', 
     'posts_per_page' => 1, 
     'meta_key'   => 'wpcf-minimum-price', 
     'orderby'   => 'meta_value_num', 
     'order'    => 'ASC' 
    )); 

    ?> 
    <?php if($the_query->have_posts()): ?> 
     <ul> 
     <?php while($the_query->have_posts()) : $the_query->the_post(); 
       $compprd = get_the_ID(); ?> 

    <?php the_title(); ?> 
    <?php 
     endwhile; ?> 
     </ul> 
    <?php endif; ?> 
    <?php wp_reset_query(); ?> 
    </div> 

は私がwpcf-最低価格 カスタムフィールドで車のための子供の記事を表示したいが、「post_parentは」機能していません。このコードは空白の出力を示しています。これで何が間違っていますか?

+0

問題 文が単純であるとき、ソリューションを提供することは困難である、「それは動作しません」 。 質問を編集して、 のどのようなことが起こるのか、実際の の結果とどのように違うのかをより詳しく説明してください。良いものを作るためのヒントについては、[ask]を参照してください。 –

+0

カスタムフィールドwpcf-minimum-priceで車の注文の子投稿を表示したいが、 'post_parent'は機能していない。このコードは空白の出力を示しています。これで何が間違っていますか? –

答えて

0

私はこれを試しませんでした。しかし、私はこれがうまくいくことを望みます。

それが動作しない場合、私にコメントを残して、私はそれを動作させるようにしようとします。

、より良い解決策がある場合も、私は専門家からのコードを見て喜んでいるでしょう:

<div> 
    <?php 
    $parent_id = 1064; 
    $args = array('child_of' => $parent_id); 

    $children_pages = get_pages($args); 

    if (count($children_pages) != 0) : 
     foreach ($children_pages as $children_page) : 
      if ($children_page->have_posts()) : 
        $args_for_posts = array('posts_per_page' => 1, 
         'post_type' => 'carvariants', 
         'orderby' => 'meta_value_num', 
         'order' => 'ASC', 
         'post_parent' => $children_page); 
        $postlist = get_posts($args_for_posts); 
        foreach ($postlist as $post) : 
         setup_postdata($post); ?> 
         <ul> 
          <?php 
          the_post(); 
          ?> 
         </ul>  
        <?php 
        endforeach; 
        wp_reset_postdata(); 
      endif; 
     endforeach; 
    else : ?> 
     <p>No content to show.</p> 
    <?php 
    endif; ?> 
</div> 
+0

Oleg9に返信いただきありがとうございますが、私には_表示する内容がありません._メッセージ。 –