wppaste
WordPress

get_bookmarks( string|array $args = '' ): object[]

Since
2.1.0
Source
wp-includes/bookmark.php:136
Retrieves the list of bookmarks.

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 $orderby is '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: -1

    Amount of bookmarks to display. Accepts any positive number or -1 for all.
  • $categorystringdefault: empty

    Comma-separated list of category IDs to include links from.
  • $category_namestringdefault: empty

    Category to retrieve links for by name.
  • $hide_invisibleint|booldefault: 1|true

    Whether to show or hide links marked as 'invisible'. Accepts 1|true or 0|false.
  • $show_updatedint|booldefault: 0|false

    Whether to display the time the bookmark was last updated. Accepts 1|true or 0|false.
  • $includestringdefault: empty

    Comma-separated list of bookmark IDs to include.
  • $excludestringdefault: empty

    Comma-separated list of bookmark IDs to exclude.
  • $searchstringdefault: empty

    Search 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

Reaches the database via ->get_results().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
116–164

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 315.

Plugin surface
1 hook

Third-party callbacks on 'get_bookmarks' run inside this call, and their cost is not bounded by anything here.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • cacheobject cachewp_cache_get()called directly
  • hookthird-party callbacksapply_filters()called directly
  • serializeserialisationserialize()called directly
  • sqldatabase query->get_results()called directly
  • querycontent queryget_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.

WhenInstructionsCalls it makes
is_array($cache) && empty($parsed_args) && empty($inclusions) && empty($exclusions) && $orderby === "rand(116–143wp_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–150wp_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–150wp_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–150wp_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–131wp_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–157wp_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–157wp_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–157wp_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–138wp_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–138wp_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–134wp_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–164wp_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–145wp_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–141wp_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–141wp_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–148wp_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

PHPCompiledExecutedBranchesNotes
8.6-dev315116–16442
8.5315116–16442
8.4315116–164425 fewer instructions than PHP 8.3
8.3320116–16742
8.2320116–167421 more instruction than PHP 8.1
8.1319116–16542
7.4319116–16542

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:

  1. apply_filters( get_bookmarks )filterline 175 (+39 into the body)

    Filters the returned list of bookmarks.

  2. apply_filters( get_bookmarks )filterline 229 (+93 into the body)

    Filters the returned list of bookmarks.

  3. apply_filters( get_bookmarks )filterline 322 (+186 into the body)

    Filters the returned list of bookmarks.

Uses · 7

Used by · 4

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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.