Detailed explanation of Get_post and get_posts function using _php techniques in WordPress development

Source: Internet
Author: User
Tags rand

Get_post ()
Get_post () functions are rarely used in general theme productions, but because we'll talk about Get_posts () Later, we have to start with this singular form. The primary role of this function is to return a specified article as an object or an array so that we can use it later. Let's take a quick look at how he uses it.

Get_post () function description
WordPress's function name is always so easy to understand, the get_post () function as its table, that is, to get an article, a specified article as an object or an array of the form returned, so that we later use.

function uses

<?php
 get_post ($post _id, $output); 
>

$post _id variable, used to set the ID of the article to be retrieved, it should be noted that this variable we can not directly give an actual value, which will be the error, we can only be called in the form of the following.

$pid = 158;
Get_post ($pid);
If it looks like Get_post (158), then the call will be an error

$post _id, the default is None, after testing, this variable does not set the error, personal feeling that this variable seems to have no default value.

$output variable is used to set the type of return data, there are three kinds, objects, associative arrays, numeric arrays.
Objects: Object
Associative arrays: array_a
Array of values: Array_n
Default value: OBJECT

Working with instances

<?php
$mypost _id = 158;
$post _id_158 = get_post ($mypost _id, array_a);
$title = $post _id_158[' post_title '];
? >

Get_posts
get_posts function, simple to say is the plural new situation of get_post, but because is the article extracts, so the use of the method is slightly different, support many parameters to select the need to extract the article, in the CMS theme often used, of course, if you are on the WordPress There is a deeper understanding, you may also be using wp_query to replace the function, this is something, today's main introduction to the Get_posts function.

get_posts function Detailed
This function belongs to the built-in functions of WordPress, which can be used to extract several assigned or random articles in WordPress.
The more simple the description of the function, the more complex to use, the need to look at the back carefully!

How to use

<?php 
$args = Array (
  ' numberposts '   => 5,
  ' offset '     => 0,
  ' category '    =>,
  ' => '     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 the necessary variable for the function
Get_posts ($args) returns the variable of the array type.

Variable parameter detailed

<?php 
$args = Array (
  /////////////////////////////
  numberposts '   =>     > 0,
 
  //category ID, multiple separated by commas, or passing an array of numbers, you can specify multiple category numbers.
  //Most of the CMS uses the focus of this function.
  ' category '    =>,
 
  //sorting rules (Note 1)
  ' by '     => ' post_date ',
 
  //Ascending, descending ' ASC '--ascending (low to High) ' DESC '--descending (high end)
  ' order '      => ' DESC ',
 
  //To display the article ID
  ' include '     =>,
 
  //To exclude the ID
  of the article ' Exclude '     =>,
 
  //custom field name
  ' Meta_key '    =>,
  //The value of the custom field, with the previous argument, to select the display matches the value of the custom field.
  ' Meta_value '   =>,
 
  //post (log)-Default, page (page),
  //attachment (attachment), any--(All)
  ' Post_type '    => ' post ',
 
  //article MIME type
  ' Post_mime_type ' =>,
 
  //To display the parent ID
  ' post_parent ' of the article   >,
 
  //Article Status
  ' Post_status '   => ' publish ');
? >

Note:

    • ' Author '--Sorting by author numeric number
    • ' Category '--sorting by category numeric number
    • ' Content '--Sort by contents
    • ' Date '--sorted by creation date
    • ' ID '--Sort by article number
    • ' Menu_order '--sorted by menu order. Only pages are available.
    • ' Mime_type '-Sort by MIME type. Only attachments are available.
    • ' Modified '--sorted by last modified time.
    • ' Name '--sort by stub.
    • ' Parent '--Sort by Parent ID
    • ' Password '--Sort by password
    • ' Rand '--arbitrary sort result
    • ' Status '--sorted by state
    • ' title '--Sort by title
    • ' Type '--Sort by type

Instance
We just talked about using an array to pass the argument, of course we can also use a string to give the function a reference, and here's a simple example.

<?php
$posts _rand = get_posts (' Numberposts=3&orderby=rand ');
? >

The above code is used to get 3 articles randomly in WordPress.

Summarize
in fact, query_posts () and get_posts () functions, accept most of the parameters, use the same structure of the database query statements, and can achieve the same purpose, but some of the topic authors hint that query_posts () may disrupt the WordPress main loop, Therefore, it is not recommended to use here.
Get_posts use is not difficult, difficult to get the article after how to display in the page, here you need to have a certain knowledge of PHP.
If you want to display the contents of the query outside of the article loop, look at the Setup_postdata function, which can be a great help to those children's shoes that are accustomed to the template label.

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.