サポート » 使い方全般 » 固定ページの子ページ一覧を表示し、関連する最新投稿日時でソートした

  • お世話になります。WPもまだまだの初心者でございます。
    なんとも行き詰まってしまい、皆さまのお力をお借りしたく…

    親ページに子ページの一覧を表示し、子ページのauthorが投稿した最新のお知らせ1件の内容を抜粋して表示しています。
    今は子ページ一覧を取得したときのソート順になっていますが、これをお知らせの最新投稿日時順にソートしたいと思っています。

    ▼現状
    ※固定ページの作成順にソート
    ・固定ページA内容抜粋&最新投稿抜粋 <リンク → 固定ページAへ>
    ・固定ページB内容抜粋&最新投稿抜粋 <リンク → 固定ページBへ>
    ・固定ページC内容抜粋&最新投稿抜粋 <リンク → 固定ページCへ>

    ▼理想
    ※最新投稿の投稿順にソート
    ・固定ページB内容抜粋&最新投稿抜粋 <リンク → 固定ページBへ>
    ・固定ページC内容抜粋&最新投稿抜粋 <リンク → 固定ページCへ>
    ・固定ページA内容抜粋&最新投稿抜粋 <リンク → 固定ページAへ>

    最新投稿の一覧を元に固定ページの情報を取得しようと思いましたが、こちらも挫折してしまいました…

    一応ソースを添付します。

    `
    <?php if ( $post->post_parent == 0 ) : ?>
    <?php $child_posts = new WP_Query(array( ‘post_type’ => ‘page’, ‘post_parent’ => $post->child_of=36, ‘orderby’=>’post_date’ ) );?>
    <?php if( $child_posts->have_posts() ): ?>
    <?php while ($child_posts->have_posts()) : $child_posts->the_post(); ?>
    <?php $author_id = $post->post_author; ?>

    <a href=”<?php the_permalink() ?>”>
    <?php $shop_name =get_the_title(); ?>
    <!– お知らせの取得 –>
    <?php $counts = 1; $recent = new WP_Query(“author=$author_id&showposts=$counts”);
    if($recent->have_posts()) : while($recent->have_posts()) : $recent->the_post(); ?>
    <?php $post_time =get_the_time(’20y.m.d(D)H:i’); ?>
    <?php the_title(); ?>
    <?php echo mb_substr(get_the_excerpt(), 0, 40); ?>…
    <?php echo $shop_name;?> – <?php echo $post_time;?>
    </a>
    <?php endwhile; else: ?>
    <?php _e(‘お知らせはありません’); ?><?php echo $shop_name;?>
    </a>
    <?php endif; ?>
    <?php endwhile; wp_reset_query(); ?>
    <?php endif; ?>
    <?php endif; ?>
    `

    わかりにくい説明で申し訳ありませんが、ご教授頂ければ幸いです。

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • 公式ドキュメントによると、orderby に post_date というオプションは無いですね。
    公式に記載されている、datemodified を使ってみてはいかがでしょう。
    http://wpdocs.sourceforge.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/WP_Query

    orderby (string) – Sort retrieved posts by parameter. Defaults to ‘date’.
    ‘none’ – No order (available with Version 2.8).
    ‘ID’ – Order by post id. Note the captialization.
    ‘author’ – Order by author.
    ‘title’ – Order by title.
    ‘date’ – Order by date.
    ‘modified’ – Order by last modified date.
    ‘parent’ – Order by post/page parent id.
    ‘rand’ – Random order.
    ‘comment_count’ – Order by number of comments (available with Version 2.9).
    ‘menu_order’ – Order by Page Order. Used most often for Pages (Order field in the Edit Page Attributes box) and for Attachments (the integer fields in the Insert / Upload Media Gallery dialog), but could be used for any post type with distinct ‘menu_order’ values (they all default to 0).
    ‘meta_value’ – Note that a ‘meta_key=keyname’ must also be present in the query. Note also that the sorting will be alphabetical which is fine for strings (i.e. words), but can be unexpected for numbers (e.g. 1, 3, 34, 4, 56, 6, etc, rather than 1, 3, 4, 6, 34, 56 as you might naturally expect).
    ‘meta_value_num’ – Order by numeric meta value (available with Version 2.8). Also note that a ‘meta_key=keyname’ must also be present in the query. This value allows for numerical sorting as noted above in ‘meta_value’.

    トピック投稿者 yakikaze

    (@yakikaze)

    ご指摘ありがとうございました!
    こちらでよくお見かけする方からの返信うれしかったです。

    しかし、どちらも修正したのですが思い通りにはなりませんでした。

    ソートをかけたい部分は最初に取得した子ページの作成日ではなく、次の条件で取得した「同じ作成者」が投稿した記事作成日でソートをしたいのです。

    おそらく、何かに代入してソートをかけなければならないと思うのですが…さっぱり分からなくなっております。

    もしご存じでしたら教えて頂ければ幸いです。

    モデレーター jim912

    (@jim912)

    yakikazeさん、こんにちは。

    以下のような感じで。

    1. ソート用の空の配列を定義
    2. 子ページを取得
    3. 子ページをループ
    4. 子ページの作成者の最新投稿を取得
    5. 最新投稿があれば、ソート用の配列に最新投稿の日時と子ページのIDをキー、子ページと最新投稿のデータを値として格納
    6. 子ページのループ終了
    7. ソート用の配列をループし、子ページの抜粋と最新投稿の抜粋を表示
    トピック投稿者 yakikaze

    (@yakikaze)

    jim912さん、はじめましてこんにちは!

    頂いたような感じで理想通りの表示になりそうです。

    PHPかなり自信ないのですが、query_postsとforeachを使っていく感じになるんでしょうか。

    なんとかがんばってやってみようと思います。
    ありがとうございます!

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • トピック「固定ページの子ページ一覧を表示し、関連する最新投稿日時でソートした」には新たに返信することはできません。