Wp_Get_Post_Categories And Wp_Get_Object_Terms Are Uncached Functions · Issue #451 · Automattic/Vip

  -  
wp_get_post_categories( int$post_id, array$args=array()): array|WP_ErrorRetrieves the danh mục of categories for a post.

Bạn đang xem: Wp_get_post_categories and wp_get_object_terms are uncached functions · issue #451 · automattic/vip


Description

Compatibility layer for themes & plugins. Also an easy layer of abstraction away from the complexity of the taxonomy layer.

Top ↑

See also

Top ↑

Parameters

$post_idintOptional
Category query parameters. See WP_Term_Query::__construct() for supported arguments.More Arguments from WP_Term_Query::__construct( ... $query )Array or query string of term query parameters. Taxonomy string|string<>
Field(s) to lớn order terms by. Accepts:Term fields ("name", "slug", "term_group", "term_id", "id", "description", "parent", "term_order"). Unless $object_ids is not empty, "term_order" is treated the same as "term_id"."count" lớn use the number of objects associated with the term."include" khổng lồ match the "order" of the $include param."slug__in" to match the "order" of the $slug param."meta_value""meta_value_num".The value of $meta_key.The array keys of $meta_query."none" lớn omit the ORDER BY clause.Default "name".
order string
Whether to lớn order terms in ascending or descending order.Accepts "ASC" (ascending) or "DESC" (descending).Default "ASC".
hide_empty bool|int
Whether to hide terms not assigned khổng lồ any posts. Accepts 1|true or 0|false. Default 1|true.
include int<>|string
Array or comma/space-separated string of term IDs khổng lồ include.Default empty array.
exclude int<>|string
Array or comma/space-separated string of term IDs lớn exclude.If $include is non-empty, $exclude is ignored.Default empty array.
exclude_tree int<>|string
Array or comma/space-separated string of term IDs to lớn exclude along with all of their descendant terms. If $include is non-empty, $exclude_tree is ignored. Mặc định empty array.
number int|string
Maximum number of terms to lớn return. Accepts ""|0 (all) or any positive number. Mặc định ""|0 (all). Lưu ý that $number may not return accurate results when coupled with $object_ids.See #41796 for details.
offset int
The number by which to offset the terms query.
fields string
Term fields to lớn query for. Accepts:"all" Returns an array of complete term objects (WP_Term<>)."all_with_object_id" Returns an array of term objects with the "object_id" param (WP_Term<>). Works only when the $object_ids parameter is populated."ids" Returns an array of term IDs (int<>)."tt_ids" Returns an array of term taxonomy IDs (int<>)."names" Returns an array of term names (string<>)."slugs" Returns an array of term slugs (string<>)."count" Returns the number of matching terms (int)."id=>parent" Returns an associative array of parent term IDs, keyed by term ID (int<>)."id=>name" Returns an associative array of term names, keyed by term ID (string<>)."id=>slug" Returns an associative array of term slugs, keyed by term ID (string<>).Default "all".
count bool
Whether khổng lồ return a term count. If true, will take precedence over $fields. Mặc định false.
name string|string<>
Name or array of names khổng lồ return term(s) for.
slug string|string<>
Slug or array of slugs to return term(s) for.
term_taxonomy_id int|int<>
Term taxonomy ID, or array of term taxonomy IDs, lớn match when querying terms.
hierarchical bool
Whether lớn include terms that have non-empty descendants (even if $hide_empty is set khổng lồ true). Default true.
search string
Search criteria to lớn match terms. Will be SQL-formatted with wildcards before and after.
name__like string
Retrieve terms with criteria by which a term is like $name__like.
description__like string
Retrieve terms where the mô tả tìm kiếm is like $description__like.
pad_counts bool
Whether to pad the quantity of a term"s children in the quantity of each term"s "count" object variable. Default false.
get string
Whether to lớn return terms regardless of ancestry or whether the terms are empty. Accepts "all" or "" (disabled). Mặc định "".
child_of int
Term ID lớn retrieve child terms of. If multiple taxonomies are passed, $child_of is ignored. Default 0.
parent int
Parent term ID to lớn retrieve direct-child terms of.
childless bool
True to lớn limit results khổng lồ terms that have no children.This parameter has no effect on non-hierarchical taxonomies.Default false.

Xem thêm: Voi Nổi Điên Giết Người Điều Khiển Và Tấn Công Khán Giả Trong Lễ Hội


cache_domain string
Unique cache key to lớn be produced when this query is stored in an object cache. Default "core".
update_term_meta_cache bool
Whether khổng lồ prime meta caches for matched terms. Mặc định true.
meta_key string|string<>
Meta key or keys to filter by.
meta_value string|string<>
Meta value or values to filter by.

Default: array()

Top ↑

Return

array|WP_Error các mục of categories. If the $fields argument passed via $args is "all" or "all_with_object_id", an array of WP_Term objects will be returned. If $fields is "ids", an array of category IDs. If $fields is "names", an array of category names.WP_Error object if "category" taxonomy doesn"t exist.

Top ↑

More Information

The results from wp_get_post_categories() aren’t cached which will result in a database điện thoại tư vấn being made every time this function is called. Use this function with care. For performance, functions like get_the_category() should be used to lớn return categories attached lớn a post.

Top ↑

Source

File: wp-includes/post.php.View all references

function wp_get_post_categories( $post_id = 0, $args = array() ) $post_id = (int) $post_id;$defaults = array( "fields" => "ids" );$args = wp_parse_args( $args, $defaults );$cats = wp_get_object_terms( $post_id, "category", $args );return $cats;View on TracView on GitHub

Top ↑

Related

Top ↑

Uses

UsesUsesDescription
wp_get_object_terms()wp-includes/taxonomy.php

Retrieves the terms associated with the given object(s), in the supplied taxonomies.

wp_parse_args()wp-includes/functions.php

Merges user defined arguments into defaults array.

Top ↑

Used By

Used ByUsed ByDescription
bulk_edit_posts()wp-admin/includes/post.php

Processes the post data for the bulk editing of posts.

wp_get_post_cats()wp-includes/deprecated.php

Retrieves a các mục of post categories.

wp_xmlrpc_server::mt_getPostCategories()wp-includes/class-wp-xmlrpc-server.php

Retrieve post categories.

wp_xmlrpc_server::mt_publishPost()wp-includes/class-wp-xmlrpc-server.php

Sets a post’s publish status khổng lồ ‘publish’.

wp_xmlrpc_server::mw_getPost()wp-includes/class-wp-xmlrpc-server.php

Retrieve post.

wp_xmlrpc_server::mw_getRecentPosts()wp-includes/class-wp-xmlrpc-server.php

Retrieve list of recent posts.

wp_xmlrpc_server::blogger_getPost()wp-includes/class-wp-xmlrpc-server.php

Retrieve post.

wp_xmlrpc_server::blogger_getRecentPosts()wp-includes/class-wp-xmlrpc-server.php

Retrieve danh sách of recent posts.

wp_xmlrpc_server::_prepare_page()wp-includes/class-wp-xmlrpc-server.php

Prepares page data for return in an XML-RPC object.

Show 4 more used byHide more used by

Top ↑

Changelog

ChangelogVersionDescription
2.1.0Introduced.

Xem thêm: Hướng Dẫn Cách Giảm Dung Lượng Ảnh Bằng Photoshop, Cách Giảm Kích Cỡ

Top ↑

User Contributed Notes


You must log in to lớn vote on the helpfulness of this noteVote results for this note: 6You must log in to lớn vote on the helpfulness of this note
*
Contributed by Codex—8 years ago

The example below shows how categories are retrieved, and then additional information is retrieved for each category.

$post_categories = wp_get_post_categories( $post_id );$cats = array();foreach($post_categories as $c)$cat = get_category( $c );$cats<> = array( "name" => $cat->name, "slug" => $cat->slug );