get_bookmarks

Retrieves the list of bookmarks.

Description

(array) get_bookmarks( (string) $args = '' ); 

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.

Returns (array)

List of bookmark row objects.

Parameters (1)

0. $args — Optional. (string) => ''
String or array of arguments to retrieve bookmarks.

Options

  • orderby (string) => 'name'

    How to order the links by. Accepts post fields.

  • order (string) => 'ASC'

    Whether to order bookmarks in ascending or descending order. Accepts ASC (ascending) or DESC (descending).

  • limit (int) => -1

    Amount of bookmarks to display. Accepts 1+ or -1 for all.

  • category (string) => ''

    Comma-separated list of category ids to include links from.

  • category_name (string) => ''

    Category to retrieve links for by name.

  • hide_invisible (int|bool) => 1

    Whether to show or hide links marked as invisible.. Accepts 1|true or 0|false.

  • show_updated (int|bool) => 0

    Whether to display the time the bookmark was last updated. Accepts 1|true or 0|false.

  • include (string) => ''

    Comma-separated list of bookmark IDs to include.

array(

    /**
     * How to order the links by. Accepts post fields.
     *
     * @type string
     * @default 'name'
     */
    'orderby' => 'name',

    /**
     * Whether to order bookmarks in ascending or descending order. Accepts 'ASC' (ascending) or
     * 'DESC' (descending).
     *
     * @type string
     * @default 'ASC'
     */
    'order' => 'ASC',

    /**
     * Amount of bookmarks to display. Accepts 1+ or -1 for all.
     *
     * @type int
     * @default -1
     */
    'limit' => -1,

    /**
     * Comma-separated list of category ids to include links from.
     *
     * @type string
     * @default ''
     */
    'category' => '',

    /**
     * Category to retrieve links for by name.
     *
     * @type string
     * @default ''
     */
    'category_name' => '',

    /**
     * Whether to show or hide links marked as 'invisible'. Accepts 1|true or 0|false.
     *
     * @type int|bool
     * @default 1
     */
    'hide_invisible' => 1,

    /**
     * Whether to display the time the bookmark was last updated. Accepts 1|true or 0|false.
     *
     * @type int|bool
     */
    'show_updated' => 0,

    /**
     * Comma-separated list of bookmark IDs to include.
     *
     * @type string
     * @default ''
     */
    'include' => ''
);        


Usage

  1. if ( !function_exists( 'get_bookmarks' ) ) { 
  2. require_once ABSPATH . WPINC . '/bookmark.php'; 
  3.  
  4. // Optional. String or array of arguments to retrieve bookmarks. 
  5. $args = array( 
  6. 'orderby' => 'name', 
  7. 'order' => 'ASC', 
  8. 'limit' => -1, 
  9. 'category' => '', 
  10. 'category_name' => '', 
  11. 'hide_invisible' => 1, 
  12. 'show_updated' => 0, 
  13. 'include' => '' 
  14. ); 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = get_bookmarks($args); 
  18.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/bookmark.php  
  1. function get_bookmarks( $args = '' ) { 
  2. global $wpdb; 
  3.  
  4. $defaults = array( 
  5. 'orderby' => 'name', 'order' => 'ASC',  
  6. 'limit' => -1, 'category' => '',  
  7. 'category_name' => '', 'hide_invisible' => 1,  
  8. 'show_updated' => 0, 'include' => '',  
  9. 'exclude' => '', 'search' => '' 
  10. ); 
  11.  
  12. $r = wp_parse_args( $args, $defaults ); 
  13.  
  14. $key = md5( serialize( $r ) ); 
  15. $cache = false; 
  16. if ( 'rand' !== $r['orderby'] && $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) { 
  17. if ( is_array( $cache ) && isset( $cache[ $key ] ) ) { 
  18. $bookmarks = $cache[ $key ]; 
  19. /** 
  20. * Filters the returned list of bookmarks. 
  21. * The first time the hook is evaluated in this file, it returns the cached 
  22. * bookmarks list. The second evaluation returns a cached bookmarks list if the 
  23. * link category is passed but does not exist. The third evaluation returns 
  24. * the full cached results. 
  25. * @since 2.1.0 
  26. * @see get_bookmarks() 
  27. * @param array $bookmarks List of the cached bookmarks. 
  28. * @param array $r An array of bookmark query arguments. 
  29. */ 
  30. return apply_filters( 'get_bookmarks', $bookmarks, $r ); 
  31.  
  32. if ( ! is_array( $cache ) ) { 
  33. $cache = array(); 
  34.  
  35. $inclusions = ''; 
  36. if ( ! empty( $r['include'] ) ) { 
  37. $r['exclude'] = ''; //ignore exclude, category, and category_name params if using include 
  38. $r['category'] = ''; 
  39. $r['category_name'] = ''; 
  40. $inclinks = preg_split( '/[\s, ]+/', $r['include'] ); 
  41. if ( count( $inclinks ) ) { 
  42. foreach ( $inclinks as $inclink ) { 
  43. if ( empty( $inclusions ) ) { 
  44. $inclusions = ' AND ( link_id = ' . intval( $inclink ) . ' '; 
  45. } else { 
  46. $inclusions .= ' OR link_id = ' . intval( $inclink ) . ' '; 
  47. if (! empty( $inclusions ) ) { 
  48. $inclusions .= ')'; 
  49.  
  50. $exclusions = ''; 
  51. if ( ! empty( $r['exclude'] ) ) { 
  52. $exlinks = preg_split( '/[\s, ]+/', $r['exclude'] ); 
  53. if ( count( $exlinks ) ) { 
  54. foreach ( $exlinks as $exlink ) { 
  55. if ( empty( $exclusions ) ) { 
  56. $exclusions = ' AND ( link_id <> ' . intval( $exlink ) . ' '; 
  57. } else { 
  58. $exclusions .= ' AND link_id <> ' . intval( $exlink ) . ' '; 
  59. if ( ! empty( $exclusions ) ) { 
  60. $exclusions .= ')'; 
  61.  
  62. if ( ! empty( $r['category_name'] ) ) { 
  63. if ( $r['category'] = get_term_by('name', $r['category_name'], 'link_category') ) { 
  64. $r['category'] = $r['category']->term_id; 
  65. } else { 
  66. $cache[ $key ] = array(); 
  67. wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); 
  68. /** This filter is documented in wp-includes/bookmark.php */ 
  69. return apply_filters( 'get_bookmarks', array(), $r ); 
  70.  
  71. $search = ''; 
  72. if ( ! empty( $r['search'] ) ) { 
  73. $like = '%' . $wpdb->esc_like( $r['search'] ) . '%'; 
  74. $search = $wpdb->prepare(" AND ( (link_url LIKE %s) OR (link_name LIKE %s) OR (link_description LIKE %s) ) ", $like, $like, $like ); 
  75.  
  76. $category_query = ''; 
  77. $join = ''; 
  78. if ( ! empty( $r['category'] ) ) { 
  79. $incategories = preg_split( '/[\s, ]+/', $r['category'] ); 
  80. if ( count($incategories) ) { 
  81. foreach ( $incategories as $incat ) { 
  82. if ( empty( $category_query ) ) { 
  83. $category_query = ' AND ( tt.term_id = ' . intval( $incat ) . ' '; 
  84. } else { 
  85. $category_query .= ' OR tt.term_id = ' . intval( $incat ) . ' '; 
  86. if ( ! empty( $category_query ) ) { 
  87. $category_query .= ") AND taxonomy = 'link_category'"; 
  88. $join = " INNER JOIN $wpdb->term_relationships AS tr ON ($wpdb->links.link_id = tr.object_id) INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id"; 
  89.  
  90. if ( $r['show_updated'] ) { 
  91. $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1, 0) as recently_updated "; 
  92. } else { 
  93. $recently_updated_test = ''; 
  94.  
  95. $get_updated = ( $r['show_updated'] ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : ''; 
  96.  
  97. $orderby = strtolower( $r['orderby'] ); 
  98. $length = ''; 
  99. switch ( $orderby ) { 
  100. case 'length': 
  101. $length = ", CHAR_LENGTH(link_name) AS length"; 
  102. break; 
  103. case 'rand': 
  104. $orderby = 'rand()'; 
  105. break; 
  106. case 'link_id': 
  107. $orderby = "$wpdb->links.link_id"; 
  108. break; 
  109. default: 
  110. $orderparams = array(); 
  111. $keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes', 'link_description' ); 
  112. foreach ( explode( ', ', $orderby ) as $ordparam ) { 
  113. $ordparam = trim( $ordparam ); 
  114.  
  115. if ( in_array( 'link_' . $ordparam, $keys ) ) { 
  116. $orderparams[] = 'link_' . $ordparam; 
  117. } elseif ( in_array( $ordparam, $keys ) ) { 
  118. $orderparams[] = $ordparam; 
  119. $orderby = implode( ', ', $orderparams ); 
  120.  
  121. if ( empty( $orderby ) ) { 
  122. $orderby = 'link_name'; 
  123.  
  124. $order = strtoupper( $r['order'] ); 
  125. if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) { 
  126. $order = 'ASC'; 
  127.  
  128. $visible = ''; 
  129. if ( $r['hide_invisible'] ) { 
  130. $visible = "AND link_visible = 'Y'"; 
  131.  
  132. $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query"; 
  133. $query .= " $exclusions $inclusions $search"; 
  134. $query .= " ORDER BY $orderby $order"; 
  135. if ( $r['limit'] != -1 ) { 
  136. $query .= ' LIMIT ' . $r['limit']; 
  137.  
  138. $results = $wpdb->get_results( $query ); 
  139.  
  140. if ( 'rand()' !== $orderby ) { 
  141. $cache[ $key ] = $results; 
  142. wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); 
  143.  
  144. /** This filter is documented in wp-includes/bookmark.php */ 
  145. return apply_filters( 'get_bookmarks', $results, $r );