반응형
Wordpress - 모든 사용자로부터 최신 사용자 지정 게시물 가져오기
어떻게 하면 모든 사용자로부터 최신 커스텀 포스트를 얻을 수 있습니까?
$args = array( 'post_type' => 'userdatax',
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => 999999 );
$query_res = new WP_Query($args);
아래 코드에 의하면 당신의 목표를 달성할 수 있습니다.
이 코드를 사용해 보십시오.
function getUserPosts()
{
$args = array(
'order' => 'ASC',
);
$users = get_users( $args );
foreach ($users as $key => $value) {
// WP_Query arguments
$args = array(
'post_type' => array( 'userdatax' ),
'post_status' => array( 'publish' ),
'author' => $value->ID,
'posts_per_page' => '-1',
'order' => 'DESC',
'orderby' => 'date',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
echo the_title();
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
}
}
add_action('init','getUserPosts');
각 사용자의 최신 게시물을 보여줘야 한다고 생각합니다.
<?php
$lastposts = get_posts( array(
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => -1
) );
//Code to check only the latest post from each user is displayed.
if ( $lastposts ) {
$auther="";
foreach ( $lastposts as $post ) :
setup_postdata( $post );
if($auther!=get_the_author()) { ?>
<!--Do your html code here -->
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_content();
$auther=get_the_author();
}
endforeach;
wp_reset_postdata();
}
?>
도움이 되길 바라요 :)
<?php
$user_id = get_current_user_id();
$args = array( 'author' => $user_id, 'post_type' => 'any' );
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
?>
그냥 posts_per_page를 그렇게 -1로 바꿉니다.
$args = array( 'post_type' => 'userdatax',
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => -1);
$query_res = new WP_Query($args);
언급URL : https://stackoverflow.com/questions/46166018/wordpress-get-latest-custom-post-from-every-user
반응형
'programing' 카테고리의 다른 글
| 개체가 존재하지 않는 경우 Springdata-repository의 삭제 방법에 예외를 두지 않도록 하려면 어떻게 해야 합니까? (0) | 2023.09.16 |
|---|---|
| Android에서 다시 로드 작업 (0) | 2023.09.16 |
| ColorStateList를 프로그래밍 방식으로 만들려면 어떻게 해야 합니까? (0) | 2023.09.16 |
| Java에서 생성된 UUID를 Oracle DB에 효율적으로 저장하는 방법은 무엇입니까?UUID에서 하이픈을 제거하거나 제거하지 않아도 됩니까? (0) | 2023.09.16 |
| -e 집합과 "$@" 실행은 도커 진입점 스크립트에서 무엇을 합니까? (0) | 2023.09.11 |