get_posts()

最后更新于:2021-11-26 22:26:35

get_posts( array$args=null)

Retrieves an array of the latest posts, or posts matching the given criteria.

参数

$args

(array) (Optional) Arguments to retrieve posts. See GC_Query::parse_query() for all available arguments.

  • ‘numberposts’
    (int) Total number of posts to retrieve. Is an alias of $posts_per_page in GC_Query. Accepts -1 for all. Default 5.
  • ‘category’
    (int|string) Category ID or comma-separated list of IDs (this or any children). Is an alias of $cat in GC_Query. Default 0.
  • ‘include’
    (int[]) An array of post IDs to retrieve, sticky posts will be included. Is an alias of $post__in in GC_Query. Default empty array.
  • ‘exclude’
    (int[]) An array of post IDs not to retrieve. Default empty array.
  • ‘suppress_filters’
    (bool) Whether to suppress filters. Default true.

Default value: null

响应

(GC_Post[]|int[]) Array of post objects or post IDs.

源文件

文件: gc-includes/post.php

function get_posts( $args = null ) {
	$defaults = array(
		'numberposts'      => 5,
		'category'         => 0,
		'orderby'          => 'date',
		'order'            => 'DESC',
		'include'          => array(),
		'exclude'          => array(),
		'meta_key'         => '',
		'meta_value'       => '',
		'post_type'        => 'post',
		'suppress_filters' => true,
	);

	$parsed_args = gc_parse_args( $args, $defaults );
	if ( empty( $parsed_args['post_status'] ) ) {
		$parsed_args['post_status'] = ( 'attachment' === $parsed_args['post_type'] ) ? 'inherit' : 'publish';
	}
	if ( ! empty( $parsed_args['numberposts'] ) && empty( $parsed_args['posts_per_page'] ) ) {
		$parsed_args['posts_per_page'] = $parsed_args['numberposts'];
	}
	if ( ! empty( $parsed_args['category'] ) ) {
		$parsed_args['cat'] = $parsed_args['category'];
	}
	if ( ! empty( $parsed_args['include'] ) ) {
		$incposts                      = gc_parse_id_list( $parsed_args['include'] );
		$parsed_args['posts_per_page'] = count( $incposts );  // Only the number of posts included.
		$parsed_args['post__in']       = $incposts;
	} elseif ( ! empty( $parsed_args['exclude'] ) ) {
		$parsed_args['post__not_in'] = gc_parse_id_list( $parsed_args['exclude'] );
	}

	$parsed_args['ignore_sticky_posts'] = true;
	$parsed_args['no_found_rows']       = true;

	$get_posts = new GC_Query;
	return $get_posts->query( $parsed_args );

}
GC_Post Object
(
    [ID] =>
    [post_author] =>
    [post_date] => 
    [post_date_gmt] => 
    [post_content] => 
    [post_title] => 
    [post_excerpt] => 
    [post_status] =>
    [comment_status] =>
    [ping_status] => 
    [post_password] => 
    [post_name] =>
    [to_ping] => 
    [pinged] => 
    [post_modified] => 
    [post_modified_gmt] =>
    [post_content_filtered] => 
    [post_parent] => 
    [guid] => 
    [menu_order] =>
    [post_type] =>
    [post_mime_type] => 
    [comment_count] =>
    [filter] =>
)
<?php
$post_list = get_posts( array(
	'orderby'    => 'menu_order',
	'sort_order' => 'asc'
) );

$posts = array();

foreach ( $post_list as $post ) {
   $posts[] += $post->ID;
}

$current = array_search( get_the_ID(), $posts );

$prevID = $posts[ $current-1 ];
$nextID = $posts[ $current+1 ];
?>

<div class="navigation">
<?php if ( ! empty( $prevID ) ): ?>
	<div class="alignleft">
		<a href="https://docs.gechiui.com/functions/get_posts/<?php echo get_permalink( $prevID ); ?>" alt="<?php echo get_the_title( $prevID ); ?>">
			<?php _e( 'Previous', 'textdomain' ); ?>
		</a>
	</div>
<?php endif;

if ( ! empty( $nextID ) ) : ?>
	<div class="alignright">
		<a href="https://docs.gechiui.com/functions/get_posts/<?php echo get_permalink( $nextID ); ?>" alt="<?php echo get_the_title( $nextID ); ?>">
			<?php _e( 'Next', 'textdomain' ); ?>
		</a>
	</div>
<?php endif; ?>
</div><!-- .navigation -->
<ul>
	<?php
	global $post;

	$myposts = get_posts( array(
		'posts_per_page' => 5,
		'offset'         => 1,
		'category'       => 1
	) );

	if ( $myposts ) {
		foreach ( $myposts as $post ) : 
			setup_postdata( $post ); ?>
			<li><a href="https://docs.gechiui.com/functions/get_posts/<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
		<?php
		endforeach;
		gc_reset_postdata();
	}
	?>
</ul>
<?php
$lastposts = get_posts( array(
	'posts_per_page' => 3
) );

if ( $lastposts ) {
	foreach ( $lastposts as $post ) :
		setup_postdata( $post ); ?>
		<h2><a href="https://docs.gechiui.com/functions/get_posts/<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
		<?php the_content(); ?>
	<?php
	endforeach; 
	gc_reset_postdata();
}
<?php
$the_slug = 'my-slug';
$args=array(
	'name'           => $the_slug,
	'post_type'      => 'post',
	'post_status'    => 'publish',
	'posts_per_page' => 1
);
$my_posts = get_posts( $args );

if ( $my_posts ) {
	printf( __( 'ID on the first post found %s', 'textdomain' ), esc_html( $my_posts[0]->ID ) );
}
<?php
$attachments = get_posts( array(
	'post_type'      => 'attachment',
	'posts_per_page' => 500,
	'post_status'    => 'any',
	'post_parent'    => null
) );

if ( $attachments ) {
	foreach ( $attachments as $post ) {
		setup_postdata( $post );
		the_title();
		the_attachment_link( $post->ID, false );
		the_excerpt();
	}
	gc_reset_postdata();
}
?>
$postslist = get_posts( array(
	'posts_per_page' => 10,
	'order'          => 'ASC',
	'orderby'        => 'title'
) );

if ( $postslist ) {
	foreach ( $postslist as $post ) :
		setup_postdata( $post );
		?>
		<div>
			<?php the_date(); ?>
			<br />
			<?php the_title(); ?>   
			<?php the_excerpt(); ?>
		</div>
	<?php
	endforeach; 
	gc_reset_postdata();
}
<ul>
	<?php
	$rand_posts = get_posts( array(
		'posts_per_page' => 5,
		'orderby'        => 'rand'
	) );
	
	if ( $rand_posts ) {
	foreach ( $rand_posts as $post ) : 
		setup_postdata( $post );
		?>
		<li><a href="https://docs.gechiui.com/functions/get_posts/<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
		<?php
	endforeach; 
	gc_reset_postdata();
	}
	?>
</ul>
$attachments = get_posts( array(
	'post_type'      => 'attachment',
	'posts_per_page' => -1,
	'post_status'    => 'any',
	'post_parent'    => $post->ID
) );

if ( $attachments ) {
	foreach ( $attachments as $attachment ) {
		echo apply_filters( 'the_title' , $attachment->post_title );
		the_attachment_link( $attachment->ID , false );
	}
}
<ul>
	<?php
	$myposts = get_posts( $array(
		'posts_per_page' => 5,
		'offset'         => 1,
		'category'       => 1
	) );

	if ( $myposts ) {
		foreach ( $myposts as $post ) :
			setup_postdata( $post );
			?>
			<li>
				<a href="https://docs.gechiui.com/functions/get_posts/<?php the_permalink(); ?>"><?php the_title(); ?></a>
			</li>
		<?php
		endforeach; 
		gc_reset_postdata();
	}
	?>
</ul>