Expand filtering of results: Using Tags

Functions used to get lists of results for widgets and other results rendering have wide array of parameters. But, considering the number of elements you can actually use for this, these functions are not all powerful. But, they can be, because they support expanding.

Throughout the GD Star Rating there are many, many filters you can use to expand the plugin. Easiest way to find them is by either looking at particular piece of code, or searching for a ‘apply_filters’ in the plugin code. These filters names usually start with ‘gdsr_’. This tutorial will show how to add tags to filter results displayed by the widget.

Very Important: Making these kinds of changes is impossible without some knowledge of PHP/WordPress/SQL. Any complex filtering can’t be done without understanding how SQL works, how data is organized in WordPress. On it’s own plugin allows a lot of things, but it can do everything or we would have widget settings 5 pages long and hundreds of parameters in function calls. I am not going to explain how filters work in WordPress or how SQL works, that is already required knowledge for this.

Widget for rating results for posts and function wp_gdsr_render_rating_results() are the same, but you can use the function anywhere in the code. They use different template, but again, they have same structure. Core of the function behind these is preparing SQL query to get viable data based on the settings (either widget settings, or from the function arguments). Functions used to prepare SQL are very complicated, and it’s not good idea to mess with them if you are not sure what you are doing. They are all located in GDSRX class in file widgetizer.php (code/db folder).

But, to know what code is actually run, you need to follow the execution a bit. In case of all templates based rendering, you only need to follow function used for specific template. Each template has a 3 code alias. For widget or results function you need to find functions that render WSR or SRR templates. SRR is actually the same as WSR. These functions are in GDSRRenderT2 (code/t2 folder), and you need function called render_wsr(). To get results you need to go to next function prepare_wsr(). This function is parsing settings and getting results. This is huge function, but you don’t need all of it. You need to modify SQL used to get the data. For that plugin uses prepare_data_retrieve() function. All these is in GDSRRenderT2 class. No matter what type of rating, this function finally get SQL and you can modify it by using gdsr_query_results filter. So, in your theme functions.php file, you can start by adding this:

[sourcecode language=”php”]<?php
add_filter("gdsr_query_results", "gdsr_filter_with_tags", 10, 2);

function gdsr_filter_with_tags($query, $widget) {
return $query;
}
?>[/sourcecode]

This code is not doing anything for now, it’s getting current query, and all settings for rendering and returns them back. Now we need to do something with that. $query has elements that are used to generate SQL query, it’s an array with select, from, where, group, order and limit. We want to connect posts to tags and filter by one or more tags, and that will narrow results down to posts that have specified tags. In first case, tags will be specified by tag ID and on line 6 is array with these ID’s. So, only posts with these tags will be included in results:

[sourcecode language=”php”]<?php
add_filter("gdsr_query_results", "gdsr_filter_with_tags", 10, 2);

function gdsr_filter_with_tags($query, $widget) {
global $wpdb;
$tags = array(19, 20, 21, 22, 23, 24);

$query["from"].= ", ".$wpdb->term_relationships." tagtr";
$query["from"].= ", ".$wpdb->term_taxonomy." tagtt";
$query["where"].= " and tagtr.object_id = p.ID ";
$query["where"].= " and tagtr.term_taxonomy_id = tagtt.term_taxonomy_id ";
$query["where"].= " and tagtt.term_id in (".join(", ", $tags).")";

return $query;
}
?>[/sourcecode]

Lines 8/9 add terms tables to query, and 10/11/12 add rules for connecting posts with tags. Now, if you prefer using tags names, we need more things added to query. Second example is connecting terms table also.

[sourcecode language=”php”]<?php
add_filter("gdsr_query_results", "gdsr_filter_with_tags", 10, 2);

function gdsr_filter_with_tags($query, $widget) {
global $wpdb;
$tags = array("actors", "admin", "ads", "music", "auto-tagger");

$query["from"].= ", ".$wpdb->term_relationships." tagtr";
$query["from"].= ", ".$wpdb->term_taxonomy." tagtt";
$query["from"].= ", ".$wpdb->terms." tagt";
$query["where"].= " and tagtr.object_id = p.ID ";
$query["where"].= " and tagtr.term_taxonomy_id = tagtt.term_taxonomy_id ";
$query["where"].= " and tagtt.term_id = tagt.term_id";
$query["where"].= " and tagt.slug in (‘".join("’, ‘", $tags)."’)";
$query["select"].= ", tagtt.term_id as gdsr_term";

return $query;
}
?>[/sourcecode]

Line 14 uses slugs, so on line 6 you need to specify slug names for tags. If you want to use full name, replace slug with name on line 14. Line 15 will add tag ID in the results, so you can use them in rendering process (not subject of this tutorial). If you don’t need that, don’t add that line.

If you want to use filter options that widget offers by default, you can do that also. So you can limit posts by category also, and with the code I wrote here, you can limit by tags also. The way these examples work here will limit all queries for each rating widget you maybe add to page. If you want only one of them limited, use $widget array to check settings for widget and apply changes only in some cases. But, remember to ALWAYS return $query at the end of function.

There is no limit in how complex query can be, but don’t go to far, extremely complex queries or incomplete queries can really slow down your page execution.

Comments

Leave a Reply