In this tutorial I will show you how to create a table that displays articles that have at least one tag in common with the article you are viewing.
First we need to create some articles that have the same tags. For example, you can create a series of articles composed as follows:
| Post title | Tag |
| Venice | Italy |
| Rome | Italy, Capital |
| Milan | Italy |
| Paris | France, Capital |
| Marseille | France |
Click on the ADMIN FORM plugin and then on the "CREATE NEW FORM" button In the name we write Related Articles . We choose 'Choose an existing table' and select wp_term_relationships
Once saved, a message will appear in the FORM tab informing us that there is no valid primary key for modifying the data. In this case we only want to view articles with similar tags and we are not working on a list that changes the data.
Let's go to the tab setting and in the Admin sidebar menu section deselect Show in admin menu.
To allow the script to display only the articles that interest us we need to create two filters. We want all articles that have at least one tag equal to that of the article being viewed to be extracted. To do this we want tr.term_taxonomy_id to be equal to one of the ids of the tags (term) of the article being viewed. To get the id of the article you are viewing you can use the shortcode [^current_post.id]
To get the list of the tag ids of an article we can use the function [^get_post_tags]. So writing in the first filter:
Select Column: ter.term_taxonomy_id, The filter type is IN (Match in array), the value is:
[^get_post_tags.term_id post_id=[^current_post.id]].
The second filter instead says that the id of the extracted articles must be different from the id of the article you are viewing. This is to avoid showing the same article you are viewing in related articles.
Select Column: ter.object_id, The filter type is != (Does NOT Equal), the value is:
[^current_post.id].
Save the settings
The shortcode to publish the list can be found at the top right.
You can publish the shortcode in the template so that it can be applied to all pages or to individual articles
Otherwise you can use php to upload the list and publish it as you like. Here is an example of loading the list at the bottom of posts. Insert the code into the template inside function.php and replace {list_id} with the id of the list you just created.
function add_dbp_list_to_content($content) {
$append = '';
if (is_single()) {
$append = "<h3>TEST RELEATED POST</h3>" . admin_form\ADFO::get_list( {list_id} );
}
return $content.$append;
}
add_filter('the_content', 'add_dbp_list_to_content');
If we try to publish the table within the articles something like this will appear:
Object_id shows the post id, now we need to display the title.
Let's open the edit of the first column (object_id).
Deselect "Keeps settings aligned with information from the same field in tab 'Form'.". This option allows you to automatically change the column settings depending on how you change the field in the Form tab.
As title we choose "Articles" while as type we choose CUSTOM.
At this point a textarea will open in which we will insert
<a href="[^LINK id=[%data.object_id]]">[^post.title id=[%data.object_id]]</a>In the following columns we select Hide so as not to make them appear. Always uncheck "keeps settings ..." first to be able to edit columns.
Here's what the final result should look like
When writing in the ADMIN FORM built-in template engine Always remember that between attributes and value there must never be spaces! So [^get_post_tags.html post_id = [%data.object_id]] for example won't work because before and after the = symbol there are two spaces.