I am using get_bookmarks()
to get bookmark links. I can get links posted in cat A or cat B, but I only want links in both A and B.
Example what I need:
I have three link categories cat#1
, cat#2
, cat#3
.
I need to get links in both cat#1 AND cat#2
not cat#1 OR cat#2
(intersection).
I am currently using:
$links = get_bookmarks(array('category' => $term_ids));
Which doesn’t work because of how get_bookmarks()
works.
$category_query = '';
$join = '';
if ( !empty($category) ) {
$incategories = preg_split('/[s,]+/',$category);
if ( count($incategories) ) {
foreach ( $incategories as $incat ) {
if (empty($category_query))
$category_query = ' AND ( tt.term_id = ' . intval($incat) . ' ';
else
$category_query .= ' OR tt.term_id = ' . intval($incat) . ' ';
}
}
}
Note from the Editor: Adding sample code for easier understanding – Bookmarks get queried with an OR
clause by default. As they’re retrieved via $wpdb->get_result()
, there’s no easy solution in filtering aside from the queried result.
Any help or idea of different approach would be much appreciated.
UPDATE
I am trying to make a new function out of get_bookmarks() function.
I changed the code that shown above to this below (changed OR to AND). But now nothing is returning. Perhaps i need to change the SQL to something else. So, I will need advice on this.
if ( !empty($category) ) {
$incategories = preg_split('/[s,]+/',$category);
if ( count($incategories) ) {
foreach ( $incategories as $incat ) {
if (empty($category_query))
$category_query = ' AND ( tt.term_id = ' . intval($incat) . ' ';
else{
// sisir
$category_query .= ' AND tt.term_id = ' . intval($incat) . ' ';
}
}
}
}
It should be as simple as that: Just get the terms from the
link_category
and use theget_terms()
argument instead, which should work like you need it.Forking
get_bookmarks()
seems troublesome, I’d code out a bit (and cache if it gets slow). Something like this:Use
get_objects_in_term
to grab all objects using those terms so somethign similar to this: