get_bookmarks( string|array $args = '' ): object[]
- Since
- 2.1.0
- Source
wp-includes/bookmark.php:136
Description
Attempts to retrieve from the cache first based on MD5 hash of arguments. If that fails, then the query will be built from the arguments and executed. The results will be stored to the cache.
Compatibility
- WordPress
- since 2.1.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$argsstring|arrayoptional- String or array of arguments to retrieve bookmarks.Default:
''$orderbystringdefault: 'name'How to order the links by. Accepts 'id', 'link_id', 'name', 'link_name', 'url', 'link_url', 'visible', 'link_visible', 'rating', 'link_rating', 'owner', 'link_owner', 'updated', 'link_updated', 'notes', 'link_notes', 'description', 'link_description', 'length' and 'rand'. When$orderbyis 'length', orders by the character length of 'link_name'.$orderstringdefault: 'ASC'Whether to order bookmarks in ascending or descending order. Accepts 'ASC' (ascending) or 'DESC' (descending).$limitintdefault: -1Amount of bookmarks to display. Accepts any positive number or -1 for all.$categorystringdefault: emptyComma-separated list of category IDs to include links from.$category_namestringdefault: emptyCategory to retrieve links for by name.$hide_invisibleint|booldefault: 1|trueWhether to show or hide links marked as 'invisible'. Accepts 1|true or 0|false.$show_updatedint|booldefault: 0|falseWhether to display the time the bookmark was last updated. Accepts 1|true or 0|false.$includestringdefault: emptyComma-separated list of bookmark IDs to include.$excludestringdefault: emptyComma-separated list of bookmark IDs to exclude.$searchstringdefault: emptySearch terms. Will be SQL-formatted with wildcards before and after and searched in 'link_url', 'link_name' and 'link_description'.
Return value
object[]- List of bookmark row objects.
Performance profile
How much work a call to get_bookmarks() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 116–164
- Plugin surface
- 1 hook
- Called by
- 4
Reaches the database via ->get_results().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 315.
Third-party callbacks on 'get_bookmarks' run inside this call, and their cost is not bounded by anything here.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- cacheobject cache
wp_cache_get()called directly - hookthird-party callbacks
apply_filters()called directly - serializeserialisation
serialize()called directly - sqldatabase query
->get_results()called directly - querycontent query
get_term()one call below get_bookmarks()
Further down the call graph this can also reach option and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 16 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_bookmarks() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_array($cache) && empty($parsed_args) && empty($inclusions) && empty($exclusions) && $orderby === "rand( | 116–143 | wp_parse_args(), serialize(), md5(), wp_cache_get(), strtolower(), strtoupper(), ->get_results(), apply_filters() |
is_array($cache) && empty($parsed_args) && empty($inclusions) && empty($exclusions) && $orderby !== "rand( | 123–150 | wp_parse_args(), serialize(), md5(), wp_cache_get(), strtolower(), strtoupper(), ->get_results(), wp_cache_set(), apply_filters() |
is_array($cache) && empty($parsed_args) && empty($inclusions) && empty($exclusions) && $orderby === "rand( | 123–150 | wp_parse_args(), serialize(), md5(), wp_cache_get(), strtolower(), strtoupper(), absint(), ->get_results(), apply_filters() |
is_array($cache) && empty($parsed_args) && empty($inclusions) && empty($exclusions) && $orderby === "rand( | 123–150 | wp_parse_args(), serialize(), md5(), wp_cache_get(), strtolower(), explode(), strtoupper(), ->get_results(), apply_filters() |
is_array($cache) && empty($inclusions) && empty($exclusions) && !$incategories && empty($category_query) && !$parsed_args && $orderby === "rand( | 124–131 | wp_parse_args(), serialize(), md5(), wp_cache_get(), wp_parse_id_list(), strtolower(), strtoupper(), ->get_results(), apply_filters() |
is_array($cache) && empty($parsed_args) && empty($inclusions) && empty($exclusions) && $orderby !== "rand( | 130–157 | wp_parse_args(), serialize(), md5(), wp_cache_get(), strtolower(), strtoupper(), absint(), ->get_results(), wp_cache_set(), apply_filters() |
is_array($cache) && empty($parsed_args) && empty($inclusions) && empty($exclusions) && $orderby !== "rand( | 130–157 | wp_parse_args(), serialize(), md5(), wp_cache_get(), strtolower(), explode(), strtoupper(), ->get_results(), wp_cache_set(), apply_filters() |
is_array($cache) && empty($parsed_args) && empty($inclusions) && empty($exclusions) && $orderby === "rand( | 130–157 | wp_parse_args(), serialize(), md5(), wp_cache_get(), strtolower(), explode(), strtoupper(), absint(), ->get_results(), apply_filters() |
is_array($cache) && empty($inclusions) && empty($exclusions) && !$incategories && empty($category_query) && !$parsed_args && $orderby !== "rand( | 131–138 | wp_parse_args(), serialize(), md5(), wp_cache_get(), wp_parse_id_list(), strtolower(), strtoupper(), ->get_results(), wp_cache_set(), apply_filters() |
is_array($cache) && empty($inclusions) && empty($exclusions) && !$incategories && empty($category_query) && !$parsed_args && $orderby === "rand( | 131–138 | wp_parse_args(), serialize(), md5(), wp_cache_get(), wp_parse_id_list(), strtolower(), strtoupper(), absint(), ->get_results(), apply_filters() |
is_array($cache) && empty($inclusions) && empty($exclusions) && !$incategories && empty($category_query) && !$parsed_args && explode() && !empty($orderby) && $orderby === "rand( | 131–134 | wp_parse_args(), serialize(), md5(), wp_cache_get(), wp_parse_id_list(), strtolower(), explode(), strtoupper(), ->get_results(), apply_filters() |
is_array($cache) && empty($parsed_args) && empty($inclusions) && empty($exclusions) && $orderby !== "rand( | 137–164 | wp_parse_args(), serialize(), md5(), wp_cache_get(), strtolower(), explode(), strtoupper(), absint(), ->get_results(), wp_cache_set(), apply_filters() |
4 further outcomes, up to 148 instructions
is_array($cache) && empty($inclusions) && empty($exclusions) && !$incategories && empty($category_query) && !$parsed_args && $orderby !== "rand( | 138–145 | wp_parse_args(), serialize(), md5(), wp_cache_get(), wp_parse_id_list(), strtolower(), strtoupper(), absint(), ->get_results(), wp_cache_set(), apply_filters() |
is_array($cache) && empty($inclusions) && empty($exclusions) && !$incategories && empty($category_query) && !$parsed_args && explode() && !empty($orderby) && $orderby !== "rand( | 138–141 | wp_parse_args(), serialize(), md5(), wp_cache_get(), wp_parse_id_list(), strtolower(), explode(), strtoupper(), ->get_results(), wp_cache_set(), apply_filters() |
is_array($cache) && empty($inclusions) && empty($exclusions) && !$incategories && empty($category_query) && !$parsed_args && explode() && !empty($orderby) && $orderby === "rand( | 138–141 | wp_parse_args(), serialize(), md5(), wp_cache_get(), wp_parse_id_list(), strtolower(), explode(), strtoupper(), absint(), ->get_results(), apply_filters() |
is_array($cache) && empty($inclusions) && empty($exclusions) && !$incategories && empty($category_query) && !$parsed_args && explode() && !empty($orderby) && $orderby !== "rand( | 145–148 | wp_parse_args(), serialize(), md5(), wp_cache_get(), wp_parse_id_list(), strtolower(), explode(), strtoupper(), absint(), ->get_results(), wp_cache_set(), apply_filters() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 315 | 116–164 | 42 | |
| 8.5 | 315 | 116–164 | 42 | |
| 8.4 | 315 | 116–164 | 42 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 320 | 116–167 | 42 | |
| 8.2 | 320 | 116–167 | 42 | 1 more instruction than PHP 8.1 |
| 8.1 | 319 | 116–165 | 42 | |
| 7.4 | 319 | 116–165 | 42 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 3
3 hooks fire while get_bookmarks() runs, in this order:
- apply_filters( get_bookmarks )filterline 175 (+39 into the body)
Filters the returned list of bookmarks.
- apply_filters( get_bookmarks )filterline 229 (+93 into the body)
Filters the returned list of bookmarks.
- apply_filters( get_bookmarks )filterline 322 (+186 into the body)
Filters the returned list of bookmarks.
Uses · 7
- wp_parse_args()Merges user defined arguments into defaults array.
- wp_cache_get()Retrieves the cache contents from the cache by key and group.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_parse_id_list()Cleans up an array, comma- or space-separated list of IDs.
- get_term_by()Gets all term data from database by term field and data.
- wp_cache_set()Saves the data to the cache.
- absint()Converts a value to non-negative integer.
Used by · 4
- WP_Links_List_Table::prepare_items()
- get_linkobjects()Gets an array of link objects associated with category n.
- get_links()Gets the links associated with category by ID.
- wp_list_bookmarks()Retrieves or echoes all of the bookmarks.
Source code
function get_bookmarks( $args = '' ) { global $wpdb; $defaults = array( 'orderby' => 'name', 'order' => 'ASC', 'limit' => -1, 'category' => '', 'category_name' => '', 'hide_invisible' => 1, 'show_updated' => 0, 'include' => '', 'exclude' => '', 'search' => '', ); $parsed_args = wp_parse_args( $args, $defaults ); $key = md5( serialize( $parsed_args ) ); $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ); if ( 'rand' !== $parsed_args['orderby'] && $cache ) { if ( is_array( $cache ) && isset( $cache[ $key ] ) ) { $bookmarks = $cache[ $key ]; /** * Filters the returned list of bookmarks. * * The first time the hook is evaluated in this file, it returns the cached * bookmarks list. The second evaluation returns a cached bookmarks list if the * link category is passed but does not exist. The third evaluation returns * the full cached results. * * @since 2.1.0 * * @see get_bookmarks() * * @param array $bookmarks List of the cached bookmarks. * @param array $parsed_args An array of bookmark query arguments. */ return apply_filters( 'get_bookmarks', $bookmarks, $parsed_args ); } } if ( ! is_array( $cache ) ) { $cache = array(); } $inclusions = ''; if ( ! empty( $parsed_args['include'] ) ) { $parsed_args['exclude'] = ''; // Ignore exclude, category, and category_name params if using include. $parsed_args['category'] = ''; $parsed_args['category_name'] = ''; $inclinks = wp_parse_id_list( $parsed_args['include'] ); if ( count( $inclinks ) ) { foreach ( $inclinks as $inclink ) { if ( empty( $inclusions ) ) { $inclusions = ' AND ( link_id = ' . $inclink . ' '; } else { $inclusions .= ' OR link_id = ' . $inclink . ' '; } } } } if ( ! empty( $inclusions ) ) { $inclusions .= ')'; } $exclusions = ''; if ( ! empty( $parsed_args['exclude'] ) ) { $exlinks = wp_parse_id_list( $parsed_args['exclude'] ); if ( count( $exlinks ) ) { foreach ( $exlinks as $exlink ) { if ( empty( $exclusions ) ) { $exclusions = ' AND ( link_id <> ' . $exlink . ' '; } else { $exclusions .= ' AND link_id <> ' . $exlink . ' '; } } }Changelog
Introduced in 2.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/bookmark.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.