Wordpress - 获取每个用户的最新自定义帖子

问题描述 投票:0回答:5

如何获取每个用户的最新自定义帖子?

$args = array(  'post_type'      => 'userdatax', 
                'post_status'    => 'publish', 
                'orderby'        => 'post_date',
                'order'          => 'DESC',    
                'posts_per_page' => 999999 );  

$query_res = new WP_Query($args);
wordpress custom-post-type
5个回答
6
投票

根据我的说法,下面的代码可以实现你的目标。

试试这个代码

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');

4
投票

我认为你需要显示每个用户的最新帖子。`

<?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();
}
?>

希望有帮助:)


4
投票
<?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
}

?>

3
投票

只需将 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);

0
投票

要从 WordPress 中的每个用户获取最新的自定义帖子,您需要编写一个自定义查询来检索所需的数据。以下是有关如何实现此目标的分步指南:

  1. 确定自定义帖子类型: 首先,确定您要检索的自定义帖子类型。假设您有一个名为“user_posts”的自定义帖子类型。

  2. 编写自定义查询: 您可以使用

    WP_Query
    类来查询您的自定义帖子。下面是一个示例查询,用于获取每个用户的最新自定义帖子:

$args = array(
    'post_type' => 'user_posts', // Replace with your custom post type name
    'posts_per_page' => -1,     // Retrieve all posts
    'orderby' => 'date',        // Order by post date
    'order' => 'DESC',          // Sort in descending order (latest first)
);

$custom_query = new WP_Query($args);

// Initialize an array to store the latest posts for each user
$latest_posts_by_user = array();

if ($custom_query->have_posts()) {
    while ($custom_query->have_posts()) {
        $custom_query->the_post();
        
        // Get the author/user ID of the current post
        $author_id = get_post_field('post_author');
        
        // Check if this user's latest post has already been added
        if (isset($latest_posts_by_user[$author_id])) {
            // If the user's latest post has been added, compare dates to find the latest one
            $post_date = get_the_date('Y-m-d H:i:s');
            $latest_date = get_the_date('Y-m-d H:i:s', $latest_posts_by_user[$author_id]);
            
            if (strtotime($post_date) > strtotime($latest_date)) {
                // Update if this post is newer
                $latest_posts_by_user[$author_id] = get_the_ID();
            }
        } else {
            // If this is the first post for the user, add it
            $latest_posts_by_user[$author_id] = get_the_ID();
        }
    }
    
    // Restore the global post data
    wp_reset_postdata();
}

// Now $latest_posts_by_user contains the latest post for each user

在此代码中:

  • 'user_posts'
    替换为您的自定义帖子类型的名称。
  • 我们使用自定义查询来检索指定自定义帖子类型的所有帖子,并按日期降序对它们进行排序。
  • 我们创建一个关联数组
    $latest_posts_by_user
    ,其中根据每个用户的用户 ID 存储其最新的帖子 ID。
  • 我们会比较每个帖子的日期,以找到每个用户的最新帖子。

运行此代码后,

$latest_posts_by_user
将包含每个用户最新的自定义帖子,您可以根据需要访问帖子数据。

© www.soinside.com 2019 - 2024. All rights reserved.