Skip Navigation

[Resolved] Sort View by Taxonomy

This support ticket is created 7 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 19 replies, has 2 voices.

Last updated by Luo Yang 6 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#511830

I am trying to list my posts in a view where the date is set by three taxonomies (record month, record day, record year). Only the record year is a required field in the CRED form, that way if a user doesn't have a month and day for the record, they can still load it. The issue that I am running into concerns displaying the view for these posts in relation to their total record date.

I want records to be ordered/sorted by record year first, record month second and record day last. I have looked through the sort controls and it doesn't look like I have options do to this there. How do you recommend we proceed? The date picker was avoided as it didn't give an option for selecting just a year and moving from month to month or year to year is tedious and time consuming for our test users.

#511940

Dear David,

There isn't such a feature within Views, Views is using wordpress class WP_Query to query the posts, there isn't taxonomy option in parameter "orderby", see wordpress document:
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

Since you have already blocked the custom date field option, but it is the best option I can suggest. And there is another workaround, you can try with the nested views, it needs four level views:
1) Top level view, query terms of taxonomy "year", display the 2nd level view, pass the "year" value to 2nd level view with shortcode attribute.
2) 2nd level view, query terms of taxonomy "month", display the 3rd level view, pass the "year" and "month" value to 3rd level view with shortcode attribute.
3) 3rd level view, query terms of taxonomy "day", display the 4th level view, pass the "year", "month" and "day" value to 4th level view with shortcode attribute
4) 4th level view, list posts, filter by the taxonomy filters "year", "month" and "day".

See our document:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-attribute
wpv-attribute
This shortcode displays the value of an attribute in a View shortcode.
https://toolset.com/documentation/user-guides/filtering-views-by-taxonomy/
Value set by View shortcode attribute
https://toolset.com/documentation/user-guides/passing-arguments-to-views/
Controlling the filter with shortcode attributes

#512372

I'm working on this now and will get back to you if there is any trouble implementing.

#512443

OK, please let me know if you need more assistance for it, thanks

#514126
PC-Project-Screenshot-2.jpg
PC-Project-Screenshot-1.jpg

Here is what I have so far (in screenshots).

I tried to follow the instructions you provided, but I am having difficulty with "passing" the values to the respective views you mentioned.

The posts are showing up in the view but are not sorting properly/as intended (Year, Month, Day with the most recent dates first).

Thank you for clarifying some more!

Here is how we have everything set up right now:

Top Level View for Vehicle Records
Content Selection > Taxonomy > Record Date Years
Query Options > All Three Checked
Ordering > Term Name > Descending
Limit and Offset > No Limit > Skip first None
Query Filter > Taxonomy Term Filter > Terms with ID set by the shortcode attribute: terms
Filter Editor > [wpv-filter-start hide="false"][wpv-filter-controls][/wpv-filter-controls][wpv-filter-end]
Xxx
Third Level View for Vehicle Records
Content Selection > Taxonomy > Record Date Days
Query Options > All Three Checked
Ordering > Term Name > Descending
Limit and Offset > No Limit > Skip first None
Query Filter > Taxonomy Term Filter > Terms with ID set by the shortcode attribute: terms
Xxx
Second Level View for Vehicle Records
Content Selection > Taxonomy > Record Date Months
Query Options > All Three Checked
Ordering > Term Name > Descending
Limit and Offset > No Limit > Skip first None
Query Filter > Taxonomy Term Filter > Terms with ID set by the shortcode attribute: terms
Xxx
Fourth Level View of the Vehicle Records
Content Selection > Post types > Vehicle Records
Query Options > Don’t include current page in query result
Ordering > Post Title > Descending
Limit and Offset > No Limit > Skip first None
Query Filter > Select posts with taxonomy: Record Date Months slug in one of those set by the View shortcode attribute wpvrecorddatemonth AND Record Date Days slug in one of those set by the View shortcode attribute wpvrecorddateday AND Record Date Years slug in one of those set by the View shortcode attribute apvrecorddateyear > Post relationship filter: Select posts that are a children of the current post in the loop.
Loop Output Editor:
[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<table width="100%">
<thead>
<tr>
<th>[wpv-heading name="post-link"]Post title with a link[/wpv-heading]</th>
<th>[wpv-heading name=""]Record Date Months[/wpv-heading]</th>
<th>[wpv-heading name=""]Record Date Days[/wpv-heading]</th>
<th>[wpv-heading name=""]Record Date Years[/wpv-heading]</th>
<th>[wpv-heading name="types-field-vehicle-mileage"]Vehicle Mileage[/wpv-heading]</th>
<th>[wpv-heading name="types-field-cost"]Cost[/wpv-heading]</th>
<th>[wpv-heading name="types-field-done-by"]Done By[/wpv-heading]</th>
</tr>
</thead>
<tbody class="wpv-loop js-wpv-loop">
<wpv-loop>
<tr>
[wpv-post-body view_template="Loop item in Fourth Level View of the Vehicle Records"]
</tr>
</wpv-loop>
</tbody>
</table>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]
Templates for this View:
<td>[wpv-post-link]</td>
<td>[wpv-post-taxonomy type="record-date-month" separator=", " format="link" show="name" order="asc"]</td>
<td>[wpv-post-taxonomy type="record-date-day" separator=", " format="link" show="name" order="asc"]</td>
<td>[wpv-post-taxonomy type="record-date-year" separator=", " format="link" show="name" order="asc"]</td>
<td>[types field='vehicle-mileage'][/types]</td>
<td>[types field='cost'][/types]</td>
<td>[types field='done-by'][/types]</td>

#514236

Please provide a test site with same problem, also point out the problem page URL and view URLs, I can setup a demo for you.

#514668

Thanks for the details, I am checking in your website, will feedback if there is anything found

#514786

I just tested it in your website, since there are lots of terms in the taxonomies "Year", "Month" and "Days", the four level nested view will conduct PHP error: Run out the PHP memory.

So I setup a demo with only two level nested view in your website:
1) parent View "Top Level View for Vehicle Records", list terms of taxonomy "Record Date Years",
No filters needed
Display below child view ""
2) child view "Fourth Level View of the Vehicle Records", list posts of "Vehicle Records", filter by:
Record Date Years slug in one of those set by the View shortcode attribute recorddateyear
eg. [wpv-view name="view-name" recorddateyear="xxxx"]
and
Select posts that are a children of the current post in the loop.
3) Modify the content template "Template for Vehicles", Edit the text block "Vehicle Records", replace the views shortcode to display the top level view, from:
[wpv-view name="fourth-level-view-of-the-vehicle-records"]
To
[wpv-view name="top-level-view-for-vehicle-records"]

But I don't think it is what you want, I setup it for showing you what Views can do. you can restore it back by edit the content template "Template for Vehicles", Edit the text block "Vehicle Records",

Again, in your case, I think the custom date field is best option

#514936

Thank you for giving it a shot. This isn't what we were looking for. I am not opposed to using the toolset datepicker, but don't want to limit my users to using a calendar datepicker. Our preference is to use a text entry field, so that someone can quickly type in their date. The other limitation that steered us away from using the datepicker was that some of our users may not have the specific day or month in their records and will instead prefer to provide just a year. I imagine that wouldn't be possible even in the text entry format for the custom date field, but if it is please let me know how to achieve that.

#515101

I just had an idea and began putting it together. What if I pass the taxonomies values off to a function that combines them and updates a custom field, which in turn can be sorted as a string with views?

I tried the following but it looks like the function is not working and as a result I am only getting the default value ("0000 00-None 00") that I set up in types.

Here is the function I created:

/**
* Build Vehicle Record Date with Form 1252 and Custom Field full-record-date
**/
add_action('cred_save_data', 'build_vehicle_record_date', 10, 2);
function build_vehicle_record_date($post_id, $form_date)
{
if ($form_data['id']==1252) {
$term_list = wp_get_post_terms($post_id, 'record-date-year', array("fields" => "names"));
$terms1 = join("-",$term_list);
$term_list = wp_get_post_terms($post_id, 'record-date-month', array("fields" => "names"));
$terms2 = join("-",$term_list);
$term_list = wp_get_post_terms($post_id, 'record-date-day', array("fields" => "names"));
$terms3 = join("-",$term_list);

$record_date=$terms1.' '.$terms2.' '.$terms3;
wp_update_post(array('ID'=>$post_id, 'wpcf-full-record-date'=>$record_date,));
}
}

#515139

Yes, I assume the custom field "full-record-date" is a custom date field created by Types plugin, Types date field stores time-stamp value, you can try to modify the codes from:

wp_update_post(array('ID'=>$post_id, 'wpcf-full-record-date'=>$record_date,))

To:

$timestamp = strtotime($record_date);
update_post_meta($post_id, 'wpcf-full-record-date', $timestamp);

More help:
hidden link
https://codex.wordpress.org/Function_Reference/update_post_meta

#515897

Hi Luo,

Our custom field "full-record-date" is a single-line textfield and not a custom date field as the date field doesn't satisfy our needs for this project.

What we are trying to do is bring the values selected in each of the three taxonomies and pass them to the single-line textfield "full-record-date" when the user completes the form and "submits/save" the post. That way if user John Doe selects the following three values (1995; 04-April; 20) from the taxonomies for year, month and day it will be saved in the "full-record-date" field as follows: 1995 04-April 20.

Then we can order the Vehicle Record CPTs by the full-record-date field string values and achieve the ordering sequence we are looking for where records are listed from newest to oldest (not post date or time stamp as most records are from years ago).

#516326

Since it is a custom field, please try replace the "wp_update_post" in your codes with wordpress function update_post_meta(), and you will need to follow wordpress document to setup your custom PHP codes:
https://codex.wordpress.org/Function_Reference/update_post_meta

#517588

Hi Luo,

I modified my code to the following:

/**
* Build Vehicle Record Date
**/
add_action('cred_save_data', 'build_vehicle_record_date', 10, 2);
function build_vehicle_record_date($post_id, $record_date)
{
if ($form_data['id']==1252) {
$term_list = wp_get_post_terms($post_id, 'record-date-year', array("fields" => "names"));
$terms1 = join("-",$term_list);
$term_list = wp_get_post_terms($post_id, 'record-date-month', array("fields" => "names"));
$terms2 = join("-",$term_list);
$term_list = wp_get_post_terms($post_id, 'record-date-day', array("fields" => "names"));
$terms3 = join("-",$term_list);

$record_date=$terms1.' '.$terms2.' '.$terms3;
update_post_meta($post_id, 'wpcf-full-record-date', $record_date);
}
}

Despite the change I still can't get the update value to show up. I am getting the default value I created for the wpcf-full-record-date field which is "0000 00-None 00".

#517595

Apologies, this is what I actually have:

/**
* Build Vehicle Record Date
**/
add_action('cred_save_data', 'build_vehicle_record_date', 10, 2);
function build_vehicle_record_date($post_id, $form_data)
{
if ($form_data['id']==1252) {
$term_list = wp_get_post_terms($post_id, 'record-date-year', array("fields" => "names"));
$terms1 = join("-",$term_list);
$term_list = wp_get_post_terms($post_id, 'record-date-month', array("fields" => "names"));
$terms2 = join("-",$term_list);
$term_list = wp_get_post_terms($post_id, 'record-date-day', array("fields" => "names"));
$terms3 = join("-",$term_list);

$record_date=$terms1.' '.$terms2.' '.$terms3;
update_post_meta($post_id, 'wpcf-full-record-date', $record_date);
}
}

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.