Data Sources – Taxonomies
If you do not know what are data sources in the WPAdverts Custom Fields extension please refer to the Getting Started with Data Sources article that will explain what are data sources and why you should use them.
You can connect the Data Source to one of the taxonomies assigned to the “advert” Custom Post Type. By default, there is only one taxonomy assigned to the “advert” post type, specifically the advert_category.
If you have the Maps and Locations extension installed you will also have the advert_location taxonomy, although most of the time you will not want to use it with Data Source as the MAL extension is already handling location taxonomy.
The main advantage of using a taxonomy instead of predefined options is that for each taxonomy term WordPress will automatically generate an SEO-friendly URL.
For example, if you have a website with car classifieds you might want to have pages that will list manufacturers and models like https://example.com/model/audi/a4/.
The above URL would list all Ads that have the “a4” term assigned.
How to connect taxonomy to “advert” post type?
In order to have more taxonomies you need to register them you can do it with a third-party plugin like MB Custom Post Type, but it is also possible to register them with a short code snippet.
Adding the code below in your theme functions.php file will:
- register a new make_model taxonomy and add it to the “advert” custom post type
- add the default “tags” taxonomy (available with posts and pages only by default) to the “advert” custom post type
add_action( "init", function() { // Register new taxonomy and assign it to WPAdverts $args = array( 'label' => "Custom Taxonomy", 'hierarchical' => true, 'query_var' => true, 'rewrite' => array('slug' => 'cusom-tax') ); register_taxonomy( 'custom_tax', 'advert', $args ); register_taxonomy( 'tags', 'advert' ); }); add_action( "admin_init", function() { // hide custom taxonomies in the sidebar // Custom Fields editor will handle them remove_meta_box( 'custom_taxdiv', 'advert', 'side' ); remove_meta_box( 'tagsdiv-tags', 'advert', 'side' ); } );
Once you have the taxonomy assigned in the “Taxonomy” field you should see the “Custom Taxonomy” and “Tags” menu items inside the wp-admin / Classifieds menu.
Finally, the last step is to create a data source and assign taxonomy to it, you can do that from wp-admin / Classifieds / Options / Custom Fields / Data Sources / Add New Data Source panel.
PRO TIP: The taxonomy data sources will work only with taxonomies assigned to a post type (that is “advert” or “advert-author” if you are using Authors extension), to make sure you will not mix the data sources with post types it is best to put in the Title field something that will help you remember where the data source can be used.
For example instead of having a “Title” like “Cars” use something like “Cars [advert – cars_taxonomy]”, then when assigning the taxonomy to a field you will know this taxonomy will work with [adverts_add] and/or [adverts_list] forms.
When you select “Taxonomy” in the “Type” field the fields “Taxonomy” and “Args” will show and allow you to configure the data source.
As you can see in the Taxonomy dropdown you can select the two built-in taxonomies (Adverts Categories and Locations) and the two we have just added to the “advert” post type with the code snippet above.
While the “Taxonomy” field is self-explanatory, the Args field needs an explanation.
Args Field Explained
By default, the taxonomy data source will return all the taxonomy terms, but in some cases, you might want to limit the returned results, for example, you might want to allow users to select only some terms or hide the empty terms when using the data source for searching.
All the above and more is possible with the Args field, but how does it work exactly?
Well, when the data source queries the database for available terms actually the get_terms() function is called like this
get_terms( array( "taxonomy" => "actual_taxonomy_name", "hide_empty" => false ) );
In the Args field, you can enter additional arguments as a query string to add or overwrite params, below you can see a couple of examples with an explanation of how they will customize the options
// Args => hide_empty=1&orderby=count&order=DESC // Hide empty terms and sort by usage get_terms( array( "taxonomy" => "actual_taxonomy_name", "hide_empty" => 1, "orderby" => "count" "order" => "DESC" ) ); // Args => parent=100 // Return only children of the term with ID = 100. get_terms( array( "taxonomy" => "actual_taxonomy_name", "hide_empty" => false, "parent" => 100 ) );
Assigning Data Source to a field
Once your data source is ready, you need to assign it to a form.
Go to wp-admin / Classifieds / Options / Custom Fields / Form Schemes panel, create a new form or edit existing one.
Edit one of the fields that can use Data Source (that is Checkbox, Radio, Select or Autocomplete) in “Fill Method” select “Use registered data source”, a new field named “Data Source” will load, select in it the data source you created and save the field.
Notice the Data Source Args. field, it works exactly the same as the Args field described in the previous section, that is you can pass to it the get_terms() function params as a query string to customize the available options in this specific form scheme.