2017-09-21 5 views
0

내 페이지에 게시물을 추가하려면 Custom Field Suite 플러그인을 사용하고 있습니다. 내 페이지에서 아래 코드를 사용하여 게시물이 제목순으로 알파벳 순으로 표시됩니다 (페이지 편집기의 위젯에서는 기본적으로 사전 순으로 정렬되어 있지만 수동으로 이동할 수도 있습니다).날짜순으로 게시물 주문 방법 - wp_query 및 사용자 정의 필드에서 게시물 ID 가져 오기

그러나 게시 날짜별로 표시하고 싶습니다. 여기

내가 사용하고 사용자 정의 필드 스위트 필드 형식이다 : 당신은 다음 WP_Query에 그들을 위해 검색 배열 내부의 모든 ID에게의를 저장할 수 있습니다

// 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 – touto

+0

'p'매개 변수를 링크 한 문서를 기반으로 정보를 검색하고자 할 때만 전달됩니다. 1 개의 특정 항목이 배수가 아닙니다. 나는 내가 말한대로 섰다. 그것이 원인의 일부이다. – Kodaloid

답변

1

http://customfieldsuite.com/field-types/relationship.html

내 코드, 당신은 할 수 $ the_post1과 $ the_post를 선언하지 않고도 공간을 절약 할 수 있습니다. 이미 게시 ID가 있으므로. 미래 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

고마워,하지만이 오류가 발생합니다 : 구문 분석 오류 : 구문 오류, 예기치 않은 '배열'(T_ARRAY). $ posts = new Array();를 삭제하면 사라지고 모든 것이 작동하는 것처럼 보입니다. – touto

+0

새로운 배열입니다(); 대문자가 아니고 미안하지만 방금 고쳤습니다. –