2017-09-21 6 views
0

私はカスタムフィールドスイートプラグインを使用して自分のページに投稿を追加しています。私のページでは、以下のコードを使用して、投稿がアルファベット順にタイトルで表示されます(ページエディタのウィジェットでは、デフォルトでアルファベット順に並べられていますが、手動でも移動できます)。日付で投稿を注文する方法 - wp_queryとカスタムフィールドから投稿IDを引き出す

ただし、公開された日付で表示します。ここで

は、私が使用しているカスタムフィールドスイートフィールドタイプです:http://customfieldsuite.com/field-types/relationship.html

私のコード

// The Query 
$archive = CFS()->get('archive'); //getting posts from widget 

foreach ($archive as $post_id) { 
    $the_post1 = get_post($post_id); 
    $the_post = $the_post1->ID; 

$the_query2 = new WP_Query(array(
'p' => $the_post, 
'orderby' => 'date', //this isn't working 
'order' => 'ASC' 
)); 

// The Loop 
if ($the_query2->have_posts()) { 
    echo '<ul>'; 
    while ($the_query2->have_posts()) { 
     $the_query2->the_post(); 
     echo '<li>' . get_the_title() . '</li>'; 
    } 
    echo '</ul>'; 
    /* Restore original Post Data */ 
    wp_reset_postdata(); 
} else { 
    // no posts found 
} 
} 
+0

は、なぜあなたはそれがそこに属していないように見えますライン9上でこのコード ' 'P' => $ the_postを、'持っています。 – Kodaloid

+0

最初の例では、https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parametersを見つけました。 'p'パラメータをリンクしたドキュメントに基づいて – touto

+0

が渡されました。 1の特定のアイテムは複数ではありません。私は私が言ったことによって立つ、それは原因の一部です。 – Kodaloid

答えて

1

あなたのWP_Queryでそれらを検索、配列内のすべてのIDを格納することができ、およびすることができます$ the_post1と$ the_postを宣言せずに自分のスペースを節約しましょう。将来WP_Query Rerefenceについては

$archive = CFS()->get('archive'); //getting posts from widget 

$posts = new array(); 

foreach ($archive as $post_id) { 
    array_push($posts, $post_id); 
} 

$the_query2 = new WP_Query(array(
    'post_type' => 'post', 
    'post__in' => $posts, 
    'orderby' => 'date', //this isn't working 
    'order' => 'ASC' 
)); 

// The Loop 
if ($the_query2->have_posts()) { 
    echo '<ul>'; 
    while ($the_query2->have_posts()) { 
     $the_query2->the_post(); 
     echo '<li>' . get_the_title() . '</li>'; 
    } 
    echo '</ul>'; 
    /* Restore original Post Data */ 
    wp_reset_postdata(); 
} else { 
    // no posts found 
} 

https://codex.wordpress.org/Class_Reference/WP_Query

+0

ありがとう、私はこのエラーが表示されます:解析エラー:予期しない 'Array'(T_ARRAY)構文エラーです。 $ posts = new Array();を削除すると、それがなくなり、すべてが機能しているようです。 – touto

+0

新しい配列()でした。申し訳ありません大文字ではなく、ただそれを修正しました。 –

関連する問題