Home >Backend Development >PHP Tutorial >Analysis of get_post_custom function usage in WordPress development
Same as get_post_meta(), the custom field used to return the post deserves a function, except that the get_post_custom() function is simpler to use, and you don’t even need to set any parameters if used in a loop.
In fact, the basic implementation of the get_post_custom() function is similar to get_post_meta()~
get_post_custom() uses
get_post_custom($postid);
and only accepts one parameter
$postid article id;
Example demonstration
if (have_posts()) : while (have_posts()) : the_post(); var_dump(get_post_custom()); endwhile; endif;
output The result is as follows: (if the following fields are set)
array(4) { [“_edit_last”]=> array(1) { [0]=> string(1) “1” } [“_edit_lock”]=> array(1) { [0]=> string(12) “1342451729:1” } [“_thumbnail_id”]=> array(1) { [0]=> string(3) “228” } [“xzmeta”]=> array(2) { [0]=> string(3) “xz1” [1]=> string(3) “xz2” } }
get_post_custom_values and get_post_custom_keys
Because custom fields are divided into key values (keys) and custom field values (values), sometimes we need to obtain these two separately value, so two functions, get_post_custom_values and get_post_custom_keys, are derived from WordPress. As for the meaning, I really haven’t found much significance. Except for the certain use when deleting custom fields in batches, I really don’t think about it. Where it can be used, it may have a very important meaning in a vast CMS theme.
I wrote about the get_post_custom function and get_post_meta function before. I thought privately that there are not many functions related to custom fields anyway, so I sorted it out and simply wrote down all the functions related to custom fields, excluding functions of course. Some basic implementation code.
get_post_custom_values is used to get the value of the specified custom field of the current article and return it in the form of an array.
while (have_posts()) : the_post(); var_dump(get_post_custom_values(‘xzmeta')); endwhile; endif;
will roughly return the following results
(if the custom field is set)
array(2) { [0]=> string(3) “xz1” [1]=> string(3) “xz2” }
get_post_custom_keys is used to get the key values of all custom fields in the current article.
if (have_posts()) : while (have_posts()) : the_post(); var_dump(get_post_custom_keys()); endwhile; endif;
You will roughly get the following results:
(if the custom field is set)
array(4) { [0]=> string(10) “_edit_last” [1]=> string(10) “_edit_lock” [2]=> string(13) “_thumbnail_id” [3]=> string(6) “xzmeta” }
The above introduces the analysis of the use of get_post_custom function in WordPress development, including relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.