The get_post and get_posts functions in WordPress development are described in detail. wordpressget_post_PHP tutorial

Source: Internet
Author: User
The get_post and get_posts functions in WordPress development are described in detail. wordpressget_post. The get_post and get_posts functions in WordPress development are described in detail. when wordpressget_postget_post () is used in general theme creation, the get_post () function is rarely used, however, the following sections will explain how to use the get_post and get_posts functions in WordPress development, wordpressget_post

Get_post ()
In general theme creation, the get_post () function is rarely used, but we have to talk about this singular form first because get_posts () is described later. The main function of this function is to return a specified article in the form of an object or array, so that we can use it later. Let's take a brief look at his usage.

Get_post () function description
The WordPress function name is always so easy to understand. the get_post () function, just like its table, gets an article that returns a specified article in the form of an object or array, so that we can use it later.

Function usage

<?php get_post($post_id, $output); ?>

The $ post_id variable is used to set the ID of the document to be retrieved. Note that this variable cannot be directly assigned to an actual value, and an error is reported, we can only use the following method.

$ Pid = 158; get_post ($ pid); // if the call is like get_post (158); in this way, an error is returned.

$ Post_id. the default value is none. if this variable is not set, an error is returned. it seems that there is no default value for this variable.

The $ output variable is used to set the types of returned data. There are three types: objects, associated arrays, and numerical arrays.
OBJECT
Associated array: ARRAY_A
Value array: ARRAY_N
Default value: OBJECT

Use instance

<?php$mypost_id = 158;$post_id_158 = get_post($mypost_id, ARRAY_A);$title = $post_id_158['post_title'];?>

Get_posts
In simple terms, the get_posts function is the new situation of the complex number of get_post functions. However, because it is extracted from multiple articles, the method of use is slightly different. it supports the selection of many parameters to be extracted, it is often used in CMS themes. if you have a better understanding of WordPress, you may also use WP_Query to replace this function, today we will mainly introduce the get_posts function.

Get_posts function details
This function is a built-in function of WordPress used to extract multiple specified or random articles from WordPress.
The more simple a function is described, the more complicated it will be to use. you need to take a closer look at it later!

Usage

<?php $args = array(  'numberposts'   => 5,  'offset'     => 0,  'category'    => ,  'orderby'     => 'post_date',  'order'      => 'DESC',  'include'     => ,  'exclude'     => ,  'meta_key'    => ,  'meta_value'   => ,  'post_type'    => 'post',  'post_mime_type' => ,  'post_parent'   => ,  'post_status'   => 'publish' );$posts_array = get_posts( $args ); ?>

$ Args is a required variable of the function.
Get_posts ($ args) returns an array variable.

Variable parameters

<? Php $ args = array (// Number of articles to be extracted 'numberposts' => 10, // start position of the first article 'offset' => 0, // category ID. multiple IDs are separated by commas (,), or an array of IDS is passed. you can specify multiple category IDs. // The focus of most CMS functions. 'Category '=>, // sorting rule (note 1) 'orderby' => 'Post _ date ', // 'asc' in ascending and descending order -- 'desc' in ascending order (low to high) -- 'order' in descending order (high to the end) => 'desc ', // the ID of the article to be displayed 'include '=>, // The ID of the article to be excluded 'clude' =>, // custom field name 'meta _ key' =>, // set the value of a custom field to the value of the previous parameter. 'Meta _ value' =>, // post (log) -- default, page (page), // attachment (attachment), any -- (All) 'Post _ type' => 'post', // mime type of the article 'post _ mime_type '=>, // The parent ID of the article 'post _ parent' =>, // The post _ status' => 'Publish ');?>

Note:

  • 'Author' -- sort by author value
  • 'Category '-- sort by category value
  • 'Content' -- sort by content
  • 'Date' -- sort by creation date
  • 'Id' -- sort by document number
  • 'Menu _ order' -- sort by menu order. Only available on pages.
  • 'Mime _ type' -- sort by mime type. Only attachments are available.
  • 'Modified' -- sort by the last modification time.
  • 'Name' -- Sort By stubs.
  • 'Parent' -- sort by parent ID
  • 'Password' -- sort by password
  • 'Rand' -- any sorting result
  • 'Status' -- sort by status
  • 'Title' -- sort by title
  • 'Type' -- sort by type

Instance
We just talked about using arrays to pass parameters. of course we can also use strings to pass parameters to this function. Below is a simple example.

<?php$posts_rand = get_posts('numberposts=3&orderby=rand');?>

The above code is used to randomly obtain three articles in WordPress.

Summary
In fact, the query_posts () and get_posts () functions accept most of the parameters, use database query statements of the same structure, and achieve the same purpose, but some topic authors prompt query_posts () it may disturb the main cycle of WordPress, so it is not recommended here.
It is not difficult to use get_posts. it is difficult to display it on the page after obtaining an article. here, you need some PHP knowledge.
If you want to display the query content outside of the article loop, you can take a look at the setup_postdata function. This function will be of great help to those kids who are used to the template tag.

Articles you may be interested in:
  • Analysis of PHP functions used for obtaining recent articles in WordPress development
  • Introduction to using custom menu related PHP functions in WordPress development
  • Use of PHP functions used to retrieve search forms in WordPress
  • Use the wp_count_posts function in WordPress to count the number of articles
  • Detailed description of PHP functions that call comment templates and output comments cyclically in WordPress
  • Simple steps to add the Google search function to WordPress
  • Parse post_class and get_post_class functions in WordPress
  • Using the get_post_custom () function in WordPress development
  • Install and use the video Player plug-in Hana Flv Player in WordPress
  • The use of the wp_list_categories function in WordPress
  • Implementation of short code in WordPress development and related function usage skills

The get_post () function is rarely used when a general topic is created, but it will be used later...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.