Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to access the bookmarks created by a single user. The key is stored in the links table, attribute link_owner.

However, get_bookmarks() seems to load the entire table and all I am left with is to parse the table and filter the rows with a matching userid.

For my application I expect the number of links to be rather large (more than 10,000). Am I to old school to fear that this usage case will slow down things bigtime? What other ways are there to do this a bit more efficiently? WP_Query seems to apply only to posts. Or should I go and use SQL right away?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

It looks like the default get_bookmarks()

http://codex.wordpress.org/Function_Reference/get_bookmarks

is using this query:

SELECT * FROM wp_links  WHERE 1=1 AND link_visible = 'Y' ORDER BY link_name ASC;

You can check the function in /wp-includes/bookmark.php.

The get_bookmarks() function has limit and order parameters that you might find helpful.


The option get_bookmarks("limit=5")

gives this query:

SELECT * FROM wp_links  WHERE 1=1 AND link_visible = 'Y' ORDER BY link_name ASC LIMIT 5

The category parameter get_bookmarks("category=1") gives you this one with JOIN:

SELECT * FROM wp_links  
INNER JOIN wp_term_relationships AS tr ON (wp_links.link_id = tr.object_id) 
INNER JOIN wp_term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id 
WHERE 1=1 AND link_visible = 'Y'  AND ( tt.term_id = 1 ) AND taxonomy = 'link_category'       
ORDER BY link_name ASC;

So if you don't use the category parameters, these are just plain SELECT queries that use the $wpdb Database Object.

Edit: If you want to search for a special link_owner you can use this example:

global $wpdb;
$link_owner=1; // EDIT this value
$sql="SELECT * FROM wp_links  WHERE link_visible = 'Y' AND link_owner = %d ORDER BY link_name ASC;";
$results = $wpdb->get_results($wpdb->prepare($sql,$link_owner));
share|improve this answer
    
Thanks for the research. Looks like the SQL example is a winner since I don't want to abuse category. –  Feliz9 Feb 22 '13 at 19:02
    
ok great, good luck with your project and thanks if you are going to vote or accept for this answer ;-) –  birgire Feb 22 '13 at 19:37

Bookmarks were softly deprecated in 3.5 therefor it might not be very smart to use them any longer. The official replacement are menus but their UI doesn't fit handling many links associated to one menu.

If you don't need admin UI (or have your own) you can use menus, they are basically CPT which is not publicly queryable, and you can use for them either the menu API or the wp_query API

If you do need the option of admin access just go with your own CPT, and use WP_query to retrieve the posts/links.

share|improve this answer
    
Yeah, I've been wondering about the future of bookmarks as well. Custom post types seemed overkill and if I recall correctly WP_query still wouldn't support querying for link_owner. –  Feliz9 Feb 22 '13 at 19:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.