Search & Filter - Version 1.1.2

Version Description

  • Added support for all public and custom post types (the attachment post type is excluded) - all post types can be user searchable or predfined and hidden from the user. This allows for users to add multiple search widgets to their site which work on specific post types independantly from eachother.
  • Added offical updated documentation, created and moved to Search & Filter Docs
Download this release

Release Info

Developer DesignsAndCode
Plugin Icon 128x128 Search & Filter
Version 1.1.2
Comparing to
See all releases

Code changes from version 1.1.1 to 1.1.2

Files changed (6) hide show
  1. admin/style.css +6 -0
  2. of-admin.php +18 -4
  3. of-list-table.php +98 -8
  4. readme.txt +20 -6
  5. search-filter.php +309 -71
  6. style.css +4 -0
admin/style.css CHANGED
@@ -53,4 +53,10 @@ pre code
53
  pre .keyword
54
  {
55
  color:#dd1144;
 
 
 
 
 
 
56
  }
53
  pre .keyword
54
  {
55
  color:#dd1144;
56
+ }
57
+
58
+ code.large
59
+ {
60
+ font-size:12px;
61
+ line-height:20px;
62
  }
of-admin.php CHANGED
@@ -46,9 +46,15 @@
46
  echo "<h3>About</h3>";
47
  echo '<div class="of-caption">
48
  Search &amp; Filter is a simple search and filtering plugin for Wordpress brought to you by <a href="http://www.designsandcode.com" target="_blank">Designs &amp; Code</a>.<br /><br />
49
- It is essentially an advancement of the Wordpress search box, adding taxonomy filters to really refine your searches!<br /><br />
50
- You can search by Category, Tag, Custom Taxonomy or any combination of these easily - you can even remove the search box and simply use it as a filtering system for your posts and pages.
51
  </div>';
 
 
 
 
 
 
52
  echo "<h3>How To Use</h3>";
53
  echo '<div class="of-caption">
54
  To display Search &amp; Filter all you need to do is a use a shortcode:<br />
@@ -77,6 +83,13 @@
77
  $ofTaxListTable->prepare_items();
78
  $ofTaxListTable->display();
79
 
 
 
 
 
 
 
 
80
  echo "<h3>Styling</h3>";
81
  echo '<div class="of-caption">
82
  Search &amp; Filter uses standard inputs and selects, form elements are contained in an unordered list - styling should be easy. <a href="'.SEARCHANDFILTER_PLUGIN_URL . '/style.css'.'" target="_blank">Please see CSS file for base styles used.</a>
@@ -102,8 +115,9 @@
102
  echo "<h3>Links</h3>";
103
  echo '<div class="of-caption">
104
  <ul>
105
- <li><a href="http://www.designsandcode.com/447/wordpress-search-filter-plugin-for-taxonomies/" target="_blank">Plugin Tutorial and Support Page</a></li>
106
- <li><a href="http://wordpress.org/plugins/search-filter" target="_blank">Plugin on WordPress.org</a> (waiting for approval)</li>
 
107
  <li><a href="https://github.com/rmorse/wp-search-filter" target="_blank">Plugin on Github</a></li>
108
  </ul>
109
  </div>';
46
  echo "<h3>About</h3>";
47
  echo '<div class="of-caption">
48
  Search &amp; Filter is a simple search and filtering plugin for Wordpress brought to you by <a href="http://www.designsandcode.com" target="_blank">Designs &amp; Code</a>.<br /><br />
49
+ It is essentially an advancement of the WordPress search box, adding taxonomy and post type filters to really refine your searches.<br /><br />
50
+ You can search by Category, Tag, Custom Taxonomy, Post Type or any combination of these easily - you can even remove the search box and simply use it as a filtering system for your posts and pages. Taxonomies and Post Types can be displayed as dropdown selects, checkboxes or radio buttons.
51
  </div>';
52
+ echo "<h3>Documentation</h3>";
53
+ echo '<div class="of-caption">
54
+ Advanced documentation and examples has now moved - <a href="http://docs.designsandcode.com/search-filter/" target="_blank">find it on our official Search &amp; Filter Plugin Documentation page</a>.<br /><br />
55
+ Please find below documentation to get you started, more examples can be found at the above link.
56
+ </div>';
57
+
58
  echo "<h3>How To Use</h3>";
59
  echo '<div class="of-caption">
60
  To display Search &amp; Filter all you need to do is a use a shortcode:<br />
83
  $ofTaxListTable->prepare_items();
84
  $ofTaxListTable->display();
85
 
86
+ echo "<h3>Your Public Post Types</h3>";
87
+ echo '<div class="of-caption"><strong>Note:</strong> the <code>attachment</code> post type is not available in this list.</div>';
88
+ //Prepare Taxonomy elements
89
+ $ofPostTypeTable = new OF_Post_Type_Table();
90
+ $ofPostTypeTable->prepare_items();
91
+ $ofPostTypeTable->display();
92
+
93
  echo "<h3>Styling</h3>";
94
  echo '<div class="of-caption">
95
  Search &amp; Filter uses standard inputs and selects, form elements are contained in an unordered list - styling should be easy. <a href="'.SEARCHANDFILTER_PLUGIN_URL . '/style.css'.'" target="_blank">Please see CSS file for base styles used.</a>
115
  echo "<h3>Links</h3>";
116
  echo '<div class="of-caption">
117
  <ul>
118
+ <li><a href="http://www.designsandcode.com/447/wordpress-search-filter-plugin-for-taxonomies/" target="_blank">Plugin Support Page &amp; Discussion</a></li>
119
+ <li><a href="http://docs.designsandcode.com/search-filter/" target="_blank">Plugin Documentation</a></li>
120
+ <li><a href="http://wordpress.org/plugins/search-filter" target="_blank">Plugin on WordPress.org</a></li>
121
  <li><a href="https://github.com/rmorse/wp-search-filter" target="_blank">Plugin on Github</a></li>
122
  </ul>
123
  </div>';
of-list-table.php CHANGED
@@ -84,6 +84,84 @@ class OF_Taxonomy_List_Table extends WP_List_Table {
84
  }
85
  }
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
 
89
  class OF_Variable_List_Table extends WP_List_Table {
@@ -111,7 +189,7 @@ class OF_Variable_List_Table extends WP_List_Table {
111
  "ID" => $counter,
112
  "name" => "taxonomies",
113
  "defaultval" => "&nbsp;",
114
- "options" => "<em>Comma seperated list of any taxonomy names found in the Public Taxonomies table below.</em>",
115
  "info" => "Example using all your public taxonomies (copy &amp; paste!):<pre><code class='string'>[searchandfilter taxonomies=\"".$fulltaxonomylist."\"]</code></pre>"
116
  );
117
  $counter++;
@@ -120,8 +198,8 @@ class OF_Variable_List_Table extends WP_List_Table {
120
  $this->taxonomy_data[] = array(
121
  "ID" => $counter,
122
  "name" => "type",
123
- "defaultval" => "select",
124
- "options" => "<em>Comma seperated list of any of the types found below:</em><br /><br />select<br />checkbox<br />radio",
125
  "info" => "The order of values in this comma seperated list needs to match the taxonomies list. <br /><br />To display categories, tags and post formats, as a `select` dropdown, radio buttons and checkboxes, we must put them in the order we need:
126
  <br /><pre><code class='string'>[searchandfilter taxonomies=\"category,post_tag,post_format\" type=\"select,checkbox,radio\"]</code></pre>
127
  If any taxonomies are left unspecified they well default to `select` dropdowns:
@@ -135,8 +213,8 @@ class OF_Variable_List_Table extends WP_List_Table {
135
  $this->taxonomy_data[] = array(
136
  "ID" => $counter,
137
  "name" => "label",
138
- "defaultval" => "name",
139
- "options" => "0 - hide all labels<br /><br /> or <br /><br /><em>Comma seperated list of any of the types found below:</em><br /><br />name<br />singular_name<br />search_items<br />all_items<br /><em>*blank value</em>",
140
  "info" => "This list works the same as the `type` example above.<br /><br />
141
  The different values that can be used are taken directly from the labels within a taxonomy object - so make sure you set these in your taxonomies if you wish to use them below.
142
  <br /><br />Examples:<br /><br />
@@ -152,11 +230,23 @@ class OF_Variable_List_Table extends WP_List_Table {
152
  );
153
  $counter++;
154
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  $this->taxonomy_data[] = array(
156
  "ID" => $counter,
157
  "name" => "search",
158
- "defaultval" => "1",
159
- "options" => "0 - hide the search box<br />1 - display search box",
160
  "info" => "The search box is shown by default, ommit from shortcode unless you specifically want to hide it - then set it with a value of 0."
161
  );
162
  $counter++;
@@ -174,7 +264,7 @@ class OF_Variable_List_Table extends WP_List_Table {
174
  $this->taxonomy_data[] = array(
175
  "ID" => $counter,
176
  "name" => "submitlabel",
177
- "defaultval" => "Submit",
178
  "options" => "<em>Any string</em>",
179
  "info" => "This is the text label on the submit button."
180
  );
84
  }
85
  }
86
 
87
+ class OF_Post_Type_Table extends WP_List_Table {
88
+
89
+ private $post_types = array();
90
+
91
+ function __construct()
92
+ {
93
+ global $status, $page;
94
+ parent::__construct(array(
95
+ 'singular'=> 'wp_list_of_post_type', //Singular label
96
+ 'plural' => 'wp_list_of_post_types', //plural label, also this well be one of the table css class
97
+ 'ajax' => false //We won't support Ajax for this table
98
+ ));
99
+
100
+ $args = array('public' => true);
101
+ $output = 'object'; // names or objects, note names is the default
102
+ $operator = 'and'; // 'and' or 'or'
103
+
104
+ $post_types_objs = get_post_types( $args, $output, $operator );
105
+
106
+
107
+
108
+ if($post_types_objs)
109
+ {
110
+ $counter = 0;
111
+
112
+ foreach ( $post_types_objs as $post_type )
113
+ {
114
+ if($post_type->name!="attachment")
115
+ {
116
+ $tempobject = array(
117
+ "ID" => $counter,
118
+ "name" => $post_type->name,
119
+ "label" => $post_type->labels->name
120
+ );
121
+
122
+ $this->post_types[] = $tempobject;
123
+
124
+ }
125
+ }
126
+ }
127
+ }
128
+
129
+ function get_columns(){
130
+ $columns = array(
131
+ 'name' => 'Name',
132
+ 'label' => 'Label'
133
+ );
134
+ return $columns;
135
+ }
136
+
137
+ function prepare_items() {
138
+ $columns = $this->get_columns();
139
+ $hidden = array();
140
+ $sortable = array();
141
+ $this->_column_headers = array($columns, $hidden, $sortable);
142
+ $this->items = $this->post_types;
143
+ }
144
+
145
+ function column_default( $item, $column_name ) {
146
+ switch( $column_name )
147
+ {
148
+ case 'name':
149
+ case 'label':
150
+ return $item[ $column_name ];
151
+ default:
152
+ return print_r( $item, true ) ; //Show the whole array for troubleshooting purposes
153
+ }
154
+ }
155
+
156
+
157
+ function get_sortable_columns()
158
+ {
159
+ $sortable_columns = array(
160
+ );
161
+ return $sortable_columns;
162
+ }
163
+ }
164
+
165
 
166
 
167
  class OF_Variable_List_Table extends WP_List_Table {
189
  "ID" => $counter,
190
  "name" => "taxonomies",
191
  "defaultval" => "&nbsp;",
192
+ "options" => "<em>Comma seperated list of any taxonomy names found in the Public Taxonomies table below.</em><br /><br /><strong>Update:</strong> You can now also add <code class='string'>post_type</code> to this list to display options for post types.",
193
  "info" => "Example using all your public taxonomies (copy &amp; paste!):<pre><code class='string'>[searchandfilter taxonomies=\"".$fulltaxonomylist."\"]</code></pre>"
194
  );
195
  $counter++;
198
  $this->taxonomy_data[] = array(
199
  "ID" => $counter,
200
  "name" => "type",
201
+ "defaultval" => "<code class='string large'>select</code>",
202
+ "options" => "<em>Comma seperated list of any of the types found below:</em><br /><br /><code class='string large'>select</code><br /><code class='string large'>checkbox</code><br /><code class='string large'>radio</code>",
203
  "info" => "The order of values in this comma seperated list needs to match the taxonomies list. <br /><br />To display categories, tags and post formats, as a `select` dropdown, radio buttons and checkboxes, we must put them in the order we need:
204
  <br /><pre><code class='string'>[searchandfilter taxonomies=\"category,post_tag,post_format\" type=\"select,checkbox,radio\"]</code></pre>
205
  If any taxonomies are left unspecified they well default to `select` dropdowns:
213
  $this->taxonomy_data[] = array(
214
  "ID" => $counter,
215
  "name" => "label",
216
+ "defaultval" => "<code class='string large'>name</code>",
217
+ "options" => "<code class='string large'>0</code> - hide all labels<br /><br /> or <br /><br /><em>Comma seperated list of any of the types found below:</em><br /><br /><code class='string large'>name</code><br /><code class='string large'>singular_name</code><br /><code class='string large'>search_items</code><br /><code class='string large'>all_items</code><br /><em><code class='string large'>*blank value</code></em>",
218
  "info" => "This list works the same as the `type` example above.<br /><br />
219
  The different values that can be used are taken directly from the labels within a taxonomy object - so make sure you set these in your taxonomies if you wish to use them below.
220
  <br /><br />Examples:<br /><br />
230
  );
231
  $counter++;
232
 
233
+ $this->taxonomy_data[] = array(
234
+ "ID" => $counter,
235
+ "name" => "post_types",
236
+ "defaultval" => "&nbsp;",
237
+ "options" => "<em>Comma seperated list of any post types (names) in the Public Post Types table below.</em><br /><br /> or set to: <br /><br /><code class='string large'>all</code>",
238
+ "info" => "List all post types you want the widget to search. Leave blank for default behavious without any post type restrictions. This will use the default setting for post types you have in place which is often just <code>post</code> and <code>page</code><br /><br />
239
+
240
+ All searches will be constrained to the post types you add here.<br /><br />
241
+ If <code>post_type</code> has been added to <code>taxonomies</code> list above, then it will pull its data from this list, a user will be able to choose from all post types listed here."
242
+ );
243
+ $counter++;
244
+
245
  $this->taxonomy_data[] = array(
246
  "ID" => $counter,
247
  "name" => "search",
248
+ "defaultval" => "<code class='string large'>1</code>",
249
+ "options" => "<code class='string large'>0</code> - hide the search box<br /><code class='string large'>1</code> - display search box",
250
  "info" => "The search box is shown by default, ommit from shortcode unless you specifically want to hide it - then set it with a value of 0."
251
  );
252
  $counter++;
264
  $this->taxonomy_data[] = array(
265
  "ID" => $counter,
266
  "name" => "submitlabel",
267
+ "defaultval" => "<code class='string large'>Submit",
268
  "options" => "<em>Any string</em>",
269
  "info" => "This is the text label on the submit button."
270
  );
readme.txt CHANGED
@@ -1,10 +1,10 @@
1
  === Search & Filter ===
2
  Contributors: DesignsAndCode
3
  Donate link:
4
- Tags: category, filter, taxonomy, search, wordpress
5
  Requires at least: 3.5
6
  Tested up to: 3.6
7
- Stable tag: 1.1.1
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -32,14 +32,24 @@ Search & Filter uses the Taxonomy object and its label properly, this means when
32
 
33
  If you have used a plugin to register a custom taxonomy this info can normally be found under advanced settings.
34
 
 
 
 
 
 
35
  == Screenshots ==
36
 
37
  1. Full example of Search & Filter when used in a widget and with a combination of checkboxes, radio buttons and selects
38
  2. Minimal example of Search & Filter embedded in the header
39
  3. Minimal example of Search & Filter embedded in a widget
 
40
 
41
  == Changelog ==
42
 
 
 
 
 
43
  = 1.1.1 =
44
  * Fixed: when submitting an empty search/filter, "?s=" now gets appended to the url (an empty search) to force load a results page, previously this was redirecting to the homepage which does not work for many use cases
45
 
@@ -65,13 +75,17 @@ If you have used a plugin to register a custom taxonomy this info can normally b
65
 
66
  == Upgrade Notice ==
67
 
68
- Upgrade should be fully compatible with previous versions, however this update will automatically add headings to taxonomy dropdowns, refer to usage and examples on how to disable them.
69
 
70
  == Description ==
71
 
72
- Search & Filter is a simple search and filtering plugin for WordPress. It is essentially an advancement of the WordPress search box, adding taxonomy filters to really refine your searches.
 
 
 
 
73
 
74
- You can search by Category, Tag, Custom Taxonomy or any combination of these easily - you can even remove the search box and simply use it as a filtering system for your posts and pages. Taxonomies can be displayed as dropdown selects, checkboxes or radio buttons.
 
75
 
76
- For advanced usage & examples head over to the [plugin homepage](http://www.designsandcode.com/447/wordpress-search-filter-plugin-for-taxonomies/).
77
 
1
  === Search & Filter ===
2
  Contributors: DesignsAndCode
3
  Donate link:
4
+ Tags: category, filter, taxonomy, search, wordpress, post type
5
  Requires at least: 3.5
6
  Tested up to: 3.6
7
+ Stable tag: 1.1.2
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
32
 
33
  If you have used a plugin to register a custom taxonomy this info can normally be found under advanced settings.
34
 
35
+ = When using the checkboxes for Post Types =
36
+
37
+ Search & Filter uses the Taxonomy object and its label properly, this means when registering custom taxonomies make sure you fill out valid values for the following labels:
38
+
39
+
40
  == Screenshots ==
41
 
42
  1. Full example of Search & Filter when used in a widget and with a combination of checkboxes, radio buttons and selects
43
  2. Minimal example of Search & Filter embedded in the header
44
  3. Minimal example of Search & Filter embedded in a widget
45
+ 4. Example of Search & Filter using a post type filter
46
 
47
  == Changelog ==
48
 
49
+ = 1.1.2 =
50
+ * Added support for all public and custom post types (the `attachment` post type is excluded) - all post types can be user searchable or predfined and hidden from the user. This allows for users to add multiple search widgets to their site which work on specific post types independantly from eachother.
51
+ * Added offical updated documentation, created and moved to [Search & Filter Docs](http://docs.designsandcode.com/search-filter/)
52
+
53
  = 1.1.1 =
54
  * Fixed: when submitting an empty search/filter, "?s=" now gets appended to the url (an empty search) to force load a results page, previously this was redirecting to the homepage which does not work for many use cases
55
 
75
 
76
  == Upgrade Notice ==
77
 
78
+ Upgrade should be fully compatible with previous versions. A previous update however automatically added headings to dropdowns, refer to usage and examples on how to disable them if this is not the required behaviour.
79
 
80
  == Description ==
81
 
82
+ Search & Filter is a simple search and filtering plugin for WordPress. It is essentially an advancement of the WordPress search box, adding taxonomy and post type filters to really refine your searches.
83
+
84
+ You can search by Category, Tag, Custom Taxonomy, Post Type or any combination of these easily - you can even remove the search box and simply use it as a filtering system for your posts and pages. Taxonomies and Post Types can be displayed as dropdown selects, checkboxes or radio buttons.
85
+
86
+ = Links =
87
 
88
+ * [Search & Filter Documentation](http://docs.designsandcode.com/search-filter/)
89
+ * [Search & Filter Discussion](http://www.designsandcode.com/447/wordpress-search-filter-plugin-for-taxonomies/)
90
 
 
91
 
search-filter.php CHANGED
@@ -5,7 +5,7 @@ Plugin URI: http://www.designsandcode.com/447/wordpress-search-filter-plugin-for
5
  Description: Search and Filtering system for Pages, Posts, Categories, Tags and Taxonomies
6
  Author: Designs & Code
7
  Author URI: http://www.designsandcode.com/
8
- Version: 1.1.1
9
  Text Domain: searchandfilter
10
  License: GPLv2
11
  */
@@ -16,7 +16,7 @@ License: GPLv2
16
  * Set up Plugin Globals
17
  */
18
  if (!defined('SEARCHANDFILTER_VERSION_NUM'))
19
- define('SEARCHANDFILTER_VERSION_NUM', '1.1.1');
20
 
21
  if (!defined('SEARCHANDFILTER_THEME_DIR'))
22
  define('SEARCHANDFILTER_THEME_DIR', ABSPATH . 'wp-content/themes/' . get_template());
@@ -64,8 +64,14 @@ if ( ! class_exists( 'SearchAndFilter' ) )
64
  public function __construct()
65
  {
66
  // Set up reserved taxonomies
67
- $this->frmreserved = array(SEARCHANDFILTER_FPRE."category", SEARCHANDFILTER_FPRE."search", SEARCHANDFILTER_FPRE."post_tag", SEARCHANDFILTER_FPRE."submitted");
68
- $this->frmqreserved = array(SEARCHANDFILTER_FPRE."category_name", SEARCHANDFILTER_FPRE."s", SEARCHANDFILTER_FPRE."tag", SEARCHANDFILTER_FPRE."submitted"); //same as reserved
 
 
 
 
 
 
69
 
70
  // Add shortcode support for widgets
71
  add_shortcode('searchandfilter', array($this, 'shortcode'));
@@ -103,14 +109,23 @@ if ( ! class_exists( 'SearchAndFilter' ) )
103
  'submitlabel' => "Submit",
104
  'type' => "",
105
  'label' => "default",
106
- 'class' => ""
 
 
107
  ), $atts));
108
 
109
 
110
  $taxonomies = explode(",",$taxonomies);
 
 
 
 
 
 
111
  $this->taxonomylist = $taxonomies;
112
  $notaxonomies = count($taxonomies);
113
 
 
114
  //set default types for each taxonomy
115
  $types = explode(",",$type);
116
  if(!is_array($types))
@@ -210,8 +225,48 @@ if ( ! class_exists( 'SearchAndFilter' ) )
210
 
211
  //set all form defaults / dropdowns etc
212
  $this->set_defaults();
213
-
214
- return $this->get_search_filter_form($search, $submitlabel, $taxonomies, $types, $labels, $class);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
  }
216
 
217
  /*
@@ -220,9 +275,12 @@ if ( ! class_exists( 'SearchAndFilter' ) )
220
  public function set_defaults()
221
  {
222
  global $wp_query;
 
223
  /*var_dump($wp_query->query['category_name']);
224
  var_dump($wp_query->query['tag']);*/
225
- //var_dump($wp_query->query);
 
 
226
 
227
  $categories = array();
228
  //if(is_category())
@@ -272,6 +330,7 @@ if ( ! class_exists( 'SearchAndFilter' ) )
272
  //}
273
  $this->defaults[SEARCHANDFILTER_FPRE.'post_tag'] = $tags;
274
 
 
275
  //loop through all the query vars
276
  foreach($wp_query->query as $key=>$val)
277
  {
@@ -299,6 +358,13 @@ if ( ! class_exists( 'SearchAndFilter' ) )
299
  }
300
  }
301
 
 
 
 
 
 
 
 
302
 
303
  //now we may be on a taxonomy page
304
  /*if(is_tax())
@@ -449,61 +515,107 @@ if ( ! class_exists( 'SearchAndFilter' ) )
449
 
450
  /* TAXONOMIES */
451
  //loop through the posts - double check that it is the search form that has been posted, otherwise we could be looping through the posts submitted from an entirely unrelated form
452
- foreach($_POST as $key=>$val)
453
  {
454
-
455
- if(!in_array($key, $this->frmreserved))
456
- {//if the key is not in the reserved array (ie, on a custom taxonomy - not tags, categories, search term)
457
-
458
- // strip off all prefixes for custom taxonomies - we just want to do a redirect - no processing
459
- if (strpos($key, SEARCHANDFILTER_FPRE) === 0)
460
- {
461
- $key = substr($key, strlen(SEARCHANDFILTER_FPRE));
462
- }
463
-
464
- $the_post_tax = $val;
465
-
466
- //make the post an array for easy looping
467
- if(!is_array($val))
468
- {
469
- $post_tax[] = $the_post_tax;
470
- }
471
- else
472
- {
473
- $post_tax = $the_post_tax;
474
- }
475
- $taxarr = array();
476
-
477
- foreach ($post_tax as $tax)
478
- {
479
- $tax = esc_attr($tax);
480
- $taxobj = get_term_by('id',$tax,$key);
481
 
482
- if(isset($taxobj->slug))
 
483
  {
484
- $taxarr[] = $taxobj->slug;
485
  }
486
- }
487
-
488
- if(count($taxarr)>0)
489
- {
490
- $tags = implode("+",$taxarr);
491
-
492
- if(!$this->hasqmark)
493
  {
494
- $this->urlparams .= "?";
495
- $this->hasqmark = true;
496
  }
497
  else
498
  {
499
- $this->urlparams .= "&";
 
 
 
 
 
 
 
 
 
 
 
 
500
  }
501
- $this->urlparams .= $key."=".$tags;
502
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
503
  }
504
  }
505
  }
506
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
507
  if($this->has_search_posted)
508
  {//if the search has been posted, redirect to the newly formed url with all the right params
509
 
@@ -516,7 +628,7 @@ if ( ! class_exists( 'SearchAndFilter' ) )
516
 
517
  }
518
 
519
- public function get_search_filter_form($search, $submitlabel, $taxonomies, $types, $labels, $class)
520
  {
521
  $returnvar = '';
522
 
@@ -527,15 +639,27 @@ if ( ! class_exists( 'SearchAndFilter' ) )
527
  }
528
 
529
  $returnvar .= '
530
- <form action="" method="post" class="searchandfilter'.$addclass.'">
531
- <div>
 
 
 
 
 
 
 
 
 
 
 
 
 
532
  <ul>';
533
 
534
  if($search==1)
535
  {
536
 
537
  $clean_searchterm = (esc_attr($this->searchterm));
538
-
539
  $returnvar .= '<li><input type="text" name="ofsearch" placeholder="Search &hellip;" value="'.$clean_searchterm.'"></li>';
540
  }
541
 
@@ -544,22 +668,89 @@ if ( ! class_exists( 'SearchAndFilter' ) )
544
  foreach($taxonomies as $taxonomy)
545
  {
546
 
547
- $taxonomydata = get_taxonomy($taxonomy);
548
-
549
- if($taxonomydata)
550
- {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
551
  $returnvar .= "<li>";
552
 
 
 
 
 
 
 
 
 
553
  if($labels[$i]!="")
554
  {
555
- $returnvar .= "<h4>".$taxonomydata->labels->{$labels[$i]}."</h4>";
556
  }
557
 
558
- $taxonomychildren = get_categories('name=of'.$taxonomy.'&taxonomy='.$taxonomy);
 
 
 
 
 
 
 
559
 
560
  if($types[$i]=="select")
561
  {
562
- $returnvar .= $this->generate_select($taxonomychildren, $taxonomy, $this->tagid, $taxonomydata->labels);
563
  }
564
  else if($types[$i]=="checkbox")
565
  {
@@ -567,9 +758,40 @@ if ( ! class_exists( 'SearchAndFilter' ) )
567
  }
568
  else if($types[$i]=="radio")
569
  {
570
- $returnvar .= $this->generate_radio($taxonomychildren, $taxonomy, $this->tagid, $taxonomydata->labels);
571
  }
572
  $returnvar .= "</li>";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
573
  }
574
  $i++;
575
 
@@ -587,7 +809,7 @@ if ( ! class_exists( 'SearchAndFilter' ) )
587
 
588
  return $returnvar;
589
  }
590
- public function generate_select($dropdata, $name, $currentid = 0, $labels = null)
591
  {
592
  $returnvar = "";
593
 
@@ -596,11 +818,11 @@ if ( ! class_exists( 'SearchAndFilter' ) )
596
  {
597
  if($labels->all_items!="")
598
  {//check to see if all items has been registered in taxonomy then use this label
599
- $returnvar .= '<option class="level-0" value="0">'.$labels->all_items.'</option>';
600
  }
601
  else
602
  {//check to see if all items has been registered in taxonomy then use this label with prefix of "All"
603
- $returnvar .= '<option class="level-0" value="0">All '.$labels->name.'</option>';
604
  }
605
  }
606
 
@@ -614,7 +836,7 @@ if ( ! class_exists( 'SearchAndFilter' ) )
614
 
615
  $noselected = count($defaults);
616
 
617
- if(($noselected>0)&&(is_array($defaults)))
618
  {
619
  foreach($defaults as $defaultid)
620
  {
@@ -632,10 +854,9 @@ if ( ! class_exists( 'SearchAndFilter' ) )
632
 
633
  return $returnvar;
634
  }
635
- public function generate_checkbox($dropdata, $name, $currentid = 0, $labels = null)
636
  {
637
  $returnvar = "";
638
-
639
  foreach($dropdata as $dropdown)
640
  {
641
  $checked = "";
@@ -665,7 +886,7 @@ if ( ! class_exists( 'SearchAndFilter' ) )
665
  return $returnvar;
666
  }
667
 
668
- public function generate_radio($dropdata, $name, $currentid = 0, $labels = null)
669
  {
670
  $returnvar = "";
671
 
@@ -681,18 +902,35 @@ if ( ! class_exists( 'SearchAndFilter' ) )
681
  {
682
  $checked = ' checked="checked"';
683
  }
 
 
 
 
 
 
 
684
  }
685
  else
686
  {
687
  $checked = ' checked="checked"';
688
  }
 
 
 
 
 
 
 
 
 
 
689
  if($labels->all_items!="")
690
  {//check to see if all items has been registered in taxonomy then use this label
691
- $returnvar .= '<label><input class="postform" type="radio" name="'.SEARCHANDFILTER_FPRE.$name.'[]" value="0"'.$checked.'> '.$labels->all_items.'</label>';
692
  }
693
  else
694
  {//check to see if all items has been registered in taxonomy then use this label with prefix of "All"
695
- $returnvar .= '<label><input class="postform" type="radio" name="'.SEARCHANDFILTER_FPRE.$name.'[]" value="0"'.$checked.'> '.$labels->name.'</label>';
696
  }
697
  }
698
 
@@ -707,7 +945,7 @@ if ( ! class_exists( 'SearchAndFilter' ) )
707
 
708
  $noselected = count($defaults);
709
 
710
- if(($noselected>0)&&(is_array($defaults)))
711
  {
712
  foreach($defaults as $defaultid)
713
  {
5
  Description: Search and Filtering system for Pages, Posts, Categories, Tags and Taxonomies
6
  Author: Designs & Code
7
  Author URI: http://www.designsandcode.com/
8
+ Version: 1.1.2
9
  Text Domain: searchandfilter
10
  License: GPLv2
11
  */
16
  * Set up Plugin Globals
17
  */
18
  if (!defined('SEARCHANDFILTER_VERSION_NUM'))
19
+ define('SEARCHANDFILTER_VERSION_NUM', '1.1.2');
20
 
21
  if (!defined('SEARCHANDFILTER_THEME_DIR'))
22
  define('SEARCHANDFILTER_THEME_DIR', ABSPATH . 'wp-content/themes/' . get_template());
64
  public function __construct()
65
  {
66
  // Set up reserved taxonomies
67
+ $this->frmreserved = array(SEARCHANDFILTER_FPRE."category", SEARCHANDFILTER_FPRE."search", SEARCHANDFILTER_FPRE."post_tag", SEARCHANDFILTER_FPRE."submitted", SEARCHANDFILTER_FPRE."post_types");
68
+ $this->frmqreserved = array(SEARCHANDFILTER_FPRE."category_name", SEARCHANDFILTER_FPRE."s", SEARCHANDFILTER_FPRE."tag", SEARCHANDFILTER_FPRE."submitted", SEARCHANDFILTER_FPRE."post_types"); //same as reserved
69
+
70
+ //add query vars
71
+ add_filter('query_vars', array($this,'add_queryvars') );
72
+
73
+ //filter post type if it is set
74
+ add_filter('pre_get_posts', array($this,'filter_query_post_types'));
75
 
76
  // Add shortcode support for widgets
77
  add_shortcode('searchandfilter', array($this, 'shortcode'));
109
  'submitlabel' => "Submit",
110
  'type' => "",
111
  'label' => "default",
112
+ 'class' => "",
113
+ 'post_types' => "",
114
+ 'show_post_types' => "0"
115
  ), $atts));
116
 
117
 
118
  $taxonomies = explode(",",$taxonomies);
119
+
120
+ if($post_types!="")
121
+ {
122
+ $post_types = explode(",",$post_types);
123
+ }
124
+
125
  $this->taxonomylist = $taxonomies;
126
  $notaxonomies = count($taxonomies);
127
 
128
+
129
  //set default types for each taxonomy
130
  $types = explode(",",$type);
131
  if(!is_array($types))
225
 
226
  //set all form defaults / dropdowns etc
227
  $this->set_defaults();
228
+
229
+ return $this->get_search_filter_form($search, $submitlabel, $taxonomies, $types, $labels, $post_types, $class);
230
+ }
231
+
232
+
233
+ function add_queryvars( $qvars )
234
+ {
235
+ $qvars[] = 'post_types';
236
+ return $qvars;
237
+ }
238
+
239
+ function filter_query_post_types($query)
240
+ {
241
+ global $wp_query;
242
+
243
+ if(isset($wp_query->query['post_types']))
244
+ {
245
+ $search_all = false;
246
+
247
+ $post_types = explode("+",esc_attr(urlencode($wp_query->query['post_types'])));
248
+ if(isset($post_types[0]))
249
+ {
250
+ if(count($post_types)==1)
251
+ {
252
+ if($post_types[0]=="all")
253
+ {
254
+ $search_all = true;
255
+ }
256
+ }
257
+ }
258
+ if($search_all)
259
+ {
260
+ $post_types = get_post_types( '', 'names' );
261
+ $query->set('post_type', $post_types); //here we set the post types that we want WP to search
262
+ }
263
+ else
264
+ {
265
+ $query->set('post_type', $post_types); //here we set the post types that we want WP to search
266
+ }
267
+ }
268
+
269
+ return $query;
270
  }
271
 
272
  /*
275
  public function set_defaults()
276
  {
277
  global $wp_query;
278
+
279
  /*var_dump($wp_query->query['category_name']);
280
  var_dump($wp_query->query['tag']);*/
281
+ /*echo "<pre>";
282
+ var_dump($wp_query->query);
283
+ echo "</pre>";*/
284
 
285
  $categories = array();
286
  //if(is_category())
330
  //}
331
  $this->defaults[SEARCHANDFILTER_FPRE.'post_tag'] = $tags;
332
 
333
+ $taxs = array();
334
  //loop through all the query vars
335
  foreach($wp_query->query as $key=>$val)
336
  {
358
  }
359
  }
360
 
361
+ $post_types = array();
362
+ if(isset($wp_query->query['post_types']))
363
+ {
364
+ $post_types = explode("+",esc_attr(urlencode($wp_query->query['post_types'])));
365
+ }
366
+ $this->defaults[SEARCHANDFILTER_FPRE.'post_types'] = $post_types;
367
+
368
 
369
  //now we may be on a taxonomy page
370
  /*if(is_tax())
515
 
516
  /* TAXONOMIES */
517
  //loop through the posts - double check that it is the search form that has been posted, otherwise we could be looping through the posts submitted from an entirely unrelated form
518
+ if($this->has_search_posted)
519
  {
520
+ foreach($_POST as $key=>$val)
521
+ {
522
+ if(!in_array($key, $this->frmreserved))
523
+ {//if the key is not in the reserved array (ie, on a custom taxonomy - not tags, categories, search term)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
524
 
525
+ // strip off all prefixes for custom taxonomies - we just want to do a redirect - no processing
526
+ if (strpos($key, SEARCHANDFILTER_FPRE) === 0)
527
  {
528
+ $key = substr($key, strlen(SEARCHANDFILTER_FPRE));
529
  }
530
+
531
+ $the_post_tax = $val;
532
+
533
+ //make the post an array for easy looping
534
+ if(!is_array($val))
 
 
535
  {
536
+ $post_tax[] = $the_post_tax;
 
537
  }
538
  else
539
  {
540
+ $post_tax = $the_post_tax;
541
+ }
542
+ $taxarr = array();
543
+
544
+ foreach ($post_tax as $tax)
545
+ {
546
+ $tax = esc_attr($tax);
547
+ $taxobj = get_term_by('id',$tax,$key);
548
+
549
+ if(isset($taxobj->slug))
550
+ {
551
+ $taxarr[] = $taxobj->slug;
552
+ }
553
  }
 
554
 
555
+ if(count($taxarr)>0)
556
+ {
557
+ $tags = implode("+",$taxarr);
558
+
559
+ if(!$this->hasqmark)
560
+ {
561
+ $this->urlparams .= "?";
562
+ $this->hasqmark = true;
563
+ }
564
+ else
565
+ {
566
+ $this->urlparams .= "&";
567
+ }
568
+ $this->urlparams .= $key."=".$tags;
569
+
570
+ }
571
  }
572
  }
573
  }
574
 
575
+ /* POST TYPES */
576
+ if((isset($_POST[SEARCHANDFILTER_FPRE.'post_types']))&&($this->has_search_posted))
577
+ {
578
+
579
+ $the_post_types = ($_POST[SEARCHANDFILTER_FPRE.'post_types']);
580
+
581
+ //make the post an array for easy looping
582
+ if(!is_array($the_post_types))
583
+ {
584
+ $post_types_arr[] = $the_post_types;
585
+ }
586
+ else
587
+ {
588
+ $post_types_arr = $the_post_types;
589
+ }
590
+
591
+ $num_post_types = count($post_types_arr);
592
+
593
+ for($i=0; $i<$num_post_types; $i++)
594
+ {
595
+ if($post_types_arr[$i]=="0")
596
+ {
597
+ $post_types_arr[$i] = "all";
598
+ }
599
+ }
600
+
601
+ if(count($post_types_arr)>0)
602
+ {
603
+ $post_types = implode("+",$post_types_arr);
604
+
605
+ if(!$this->hasqmark)
606
+ {
607
+ $this->urlparams .= "?";
608
+ $this->hasqmark = true;
609
+ }
610
+ else
611
+ {
612
+ $this->urlparams .= "&";
613
+ }
614
+ $this->urlparams .= "post_types=".$post_types;
615
+
616
+ }
617
+ }
618
+
619
  if($this->has_search_posted)
620
  {//if the search has been posted, redirect to the newly formed url with all the right params
621
 
628
 
629
  }
630
 
631
+ public function get_search_filter_form($search, $submitlabel, $taxonomies, $types, $labels, $post_types, $class)
632
  {
633
  $returnvar = '';
634
 
639
  }
640
 
641
  $returnvar .= '
642
+ <form action="" method="post" class="searchandfilter'.$addclass.'">
643
+ <div>';
644
+
645
+ if(!in_array("post_types", $taxonomies))
646
+ {//then the user has not added it to the taxonomies list so the user does not want a post types drop down... so add (if any) the post types to a hidden attribute
647
+
648
+ if(($post_types!="")&&(is_array($post_types)))
649
+ {
650
+ foreach($post_types as $post_type)
651
+ {
652
+ $returnvar .= "<input type=\"hidden\" name=\"ofpost_types[]\" value=\"".$post_type."\" />";
653
+ }
654
+ }
655
+ }
656
+ $returnvar .= '
657
  <ul>';
658
 
659
  if($search==1)
660
  {
661
 
662
  $clean_searchterm = (esc_attr($this->searchterm));
 
663
  $returnvar .= '<li><input type="text" name="ofsearch" placeholder="Search &hellip;" value="'.$clean_searchterm.'"></li>';
664
  }
665
 
668
  foreach($taxonomies as $taxonomy)
669
  {
670
 
671
+ if($taxonomy == "post_types")
672
+ {//build taxonomy array
673
+
674
+ $taxonomychildren = array();
675
+ $post_type_count = count($post_types);
676
+
677
+ //then check the post types array
678
+ if(is_array($post_types))
679
+ {
680
+ if(($post_type_count==1)&&($post_types[0]=="all"))
681
+ {
682
+ $args = array('public' => true);
683
+ $output = 'object'; // names or objects, note names is the default
684
+ $operator = 'and'; // 'and' or 'or'
685
+
686
+ $post_types_objs = get_post_types( $args, $output, $operator );
687
+
688
+ $post_types = array();
689
+
690
+ foreach ( $post_types_objs as $post_type )
691
+ {
692
+ if($post_type->name!="attachment")
693
+ {
694
+ $tempobject = array();
695
+ $tempobject['term_id'] = $post_type->name;
696
+ $tempobject['cat_name'] = $post_type->labels->name;
697
+
698
+ $taxonomychildren[] = (object)$tempobject;
699
+
700
+ $post_types[] = $post_type->name;
701
+
702
+ }
703
+ }
704
+ $post_type_count = count($post_types_objs);
705
+
706
+ }
707
+ else
708
+ {
709
+ foreach($post_types as $post_type)
710
+ {
711
+ //var_dump(get_post_type_object( $post_type ));
712
+ $post_type_data = get_post_type_object( $post_type );
713
+
714
+ if($post_type_data)
715
+ {
716
+ $tempobject = array();
717
+ $tempobject['term_id'] = $post_type;
718
+ $tempobject['cat_name'] = $post_type_data->labels->name;
719
+
720
+ $taxonomychildren[] = (object)$tempobject;
721
+ }
722
+ }
723
+ }
724
+ }
725
+ $taxonomychildren = (object)$taxonomychildren;
726
+
727
  $returnvar .= "<li>";
728
 
729
+ $post_type_labels = array();
730
+ $post_type_labels['name'] = "Post Types";
731
+ $post_type_labels['singular_name'] = "Post Type";
732
+ $post_type_labels['search_items'] = "Search Post Types";
733
+ $post_type_labels['all_items'] = "All Post Types";
734
+
735
+ $post_type_labels = (object)$post_type_labels;
736
+
737
  if($labels[$i]!="")
738
  {
739
+ $returnvar .= "<h4>".$post_type_labels->name."</h4>";
740
  }
741
 
742
+ if($post_type_count>0)
743
+ {
744
+ $defaultval = implode("+",$post_types);
745
+ }
746
+ else
747
+ {
748
+ $defaultval = "all";
749
+ }
750
 
751
  if($types[$i]=="select")
752
  {
753
+ $returnvar .= $this->generate_select($taxonomychildren, $taxonomy, $this->tagid, $post_type_labels, $defaultval);
754
  }
755
  else if($types[$i]=="checkbox")
756
  {
758
  }
759
  else if($types[$i]=="radio")
760
  {
761
+ $returnvar .= $this->generate_radio($taxonomychildren, $taxonomy, $this->tagid, $post_type_labels, $defaultval);
762
  }
763
  $returnvar .= "</li>";
764
+
765
+ }
766
+ else
767
+ {
768
+ $taxonomydata = get_taxonomy($taxonomy);
769
+
770
+ if($taxonomydata)
771
+ {
772
+ $returnvar .= "<li>";
773
+
774
+ if($labels[$i]!="")
775
+ {
776
+ $returnvar .= "<h4>".$taxonomydata->labels->{$labels[$i]}."</h4>";
777
+ }
778
+
779
+ $taxonomychildren = get_categories('name=of'.$taxonomy.'&taxonomy='.$taxonomy);
780
+
781
+ if($types[$i]=="select")
782
+ {
783
+ $returnvar .= $this->generate_select($taxonomychildren, $taxonomy, $this->tagid, $taxonomydata->labels);
784
+ }
785
+ else if($types[$i]=="checkbox")
786
+ {
787
+ $returnvar .= $this->generate_checkbox($taxonomychildren, $taxonomy, $this->tagid);
788
+ }
789
+ else if($types[$i]=="radio")
790
+ {
791
+ $returnvar .= $this->generate_radio($taxonomychildren, $taxonomy, $this->tagid, $taxonomydata->labels);
792
+ }
793
+ $returnvar .= "</li>";
794
+ }
795
  }
796
  $i++;
797
 
809
 
810
  return $returnvar;
811
  }
812
+ public function generate_select($dropdata, $name, $currentid = 0, $labels = null, $defaultval = "0")
813
  {
814
  $returnvar = "";
815
 
818
  {
819
  if($labels->all_items!="")
820
  {//check to see if all items has been registered in taxonomy then use this label
821
+ $returnvar .= '<option class="level-0" value="'.$defaultval.'">'.$labels->all_items.'</option>';
822
  }
823
  else
824
  {//check to see if all items has been registered in taxonomy then use this label with prefix of "All"
825
+ $returnvar .= '<option class="level-0" value="'.$defaultval.'">All '.$labels->name.'</option>';
826
  }
827
  }
828
 
836
 
837
  $noselected = count($defaults);
838
 
839
+ if(($noselected==1)&&(is_array($defaults))) //there should never be more than 1 default in a select, if there are then don't set any, user is obviously searching multiple values, in the case of a select this must be "all"
840
  {
841
  foreach($defaults as $defaultid)
842
  {
854
 
855
  return $returnvar;
856
  }
857
+ public function generate_checkbox($dropdata, $name, $currentid = 0, $labels = null, $defaultval = '')
858
  {
859
  $returnvar = "";
 
860
  foreach($dropdata as $dropdown)
861
  {
862
  $checked = "";
886
  return $returnvar;
887
  }
888
 
889
+ public function generate_radio($dropdata, $name, $currentid = 0, $labels = null, $defaultval = "0")
890
  {
891
  $returnvar = "";
892
 
902
  {
903
  $checked = ' checked="checked"';
904
  }
905
+ else if($noselected==1)
906
+ {
907
+ if($this->defaults[SEARCHANDFILTER_FPRE.$name][0]==$defaultval)
908
+ {
909
+ $checked = ' checked="checked"';
910
+ }
911
+ }
912
  }
913
  else
914
  {
915
  $checked = ' checked="checked"';
916
  }
917
+
918
+ if(isset($this->defaults[SEARCHANDFILTER_FPRE.$name]))
919
+ {
920
+ $defaults = $this->defaults[SEARCHANDFILTER_FPRE.$name];
921
+ if(count($defaults)>1)
922
+ {//then we are dealing with multiple defaults - this means mutliple radios are selected, this is only possible with "ALL" so set as default.
923
+ $checked = ' checked="checked"';
924
+ }
925
+ }
926
+
927
  if($labels->all_items!="")
928
  {//check to see if all items has been registered in taxonomy then use this label
929
+ $returnvar .= '<label><input class="postform" type="radio" name="'.SEARCHANDFILTER_FPRE.$name.'[]" value="'.$defaultval.'"'.$checked.'> '.$labels->all_items.'</label>';
930
  }
931
  else
932
  {//check to see if all items has been registered in taxonomy then use this label with prefix of "All"
933
+ $returnvar .= '<label><input class="postform" type="radio" name="'.SEARCHANDFILTER_FPRE.$name.'[]" value="'.$defaultval.'"'.$checked.'> '.$labels->name.'</label>';
934
  }
935
  }
936
 
945
 
946
  $noselected = count($defaults);
947
 
948
+ if(($noselected==1)&&(is_array($defaults)))
949
  {
950
  foreach($defaults as $defaultid)
951
  {
style.css CHANGED
@@ -1,4 +1,8 @@
1
  /* Search & Filter Styles */
 
 
 
 
2
  .searchandfilter p
3
  {
4
  margin-top: 1em;
1
  /* Search & Filter Styles */
2
+ body
3
+ {
4
+ display:block;
5
+ }
6
  .searchandfilter p
7
  {
8
  margin-top: 1em;