gdtt_get_posts

gdtt_get_posts($args = null, $terms = array(), $return = "posts")

  • $args: standard arguments used for normal WordPress get_posts or query_posts functions.
  • $terms: array with taxonomies and terms for filtering. See bellow for format.
  • $return: what to return, array with posts or the WP_Query object. Values allowed are posts | query.

This function extends normal get_posts function and allows for filtering using multiple taxonomies with multiple terms for each taxonomy. This filter is specified with second parameter for function call. Each element of the $terms array is another array:

array("tax" => "", "hierarchy" => true, "field" => "term_id", "terms" => array())

First element in the array is tax and value needs to be name of the taxonomy. Hierarchy specifies if you want filtering to be hierarchical. If set to false, only given terms will be used, and parent/child hierarchy ignored. Field specifies in what format terms are given. If you add terms as ID’s, field should be term_id. If you add terms with their slug name, field should be slug.

Following code will filter posts by post tag, and will return all posts that have both wordpress and gd-press-tools tags.

[php]
$terms_filter = array(array("tax" => "post_tag",
"field" => "slug",
"terms" => array("wordpress", "gd-press-tools")
);
gdtt_get_posts($args = "post_type=post", $terms = $terms_filter, $return = "posts");[/php]

This code will filter posts by post tag, and will return all posts that have both wordpress and gd-press-tools tags and in the same time filter by category and include only categories by ID’s 1, 5 and 12.

[php]
$terms_filter = array(array("tax" => "post_tag",
"field" => "slug",
"terms" => array("wordpress", "gd-press-tools"),
array("tax" => "category",
"hierarchy" => false,
"field" => "term_id",
"terms" => array(1, 5, 12)
);
gdtt_get_posts($args = "post_type=post", $terms = $terms_filter, $return = "posts");[/php]

Comments

Leave a Reply