Ability to ORDERBY a custom field in a View?
- This topic has 10 replies, 3 voices, and was last updated 3 years, 9 months ago by
JonS.
-
AuthorPosts
-
June 28, 2021 at 7:14 PM #29174
wpstudio
ParticipantIn my arguments prior to passing into the mb.get_posts( args )... I'm running into an issue that appears to have the custom field being ignored.
{% set args = { post_type: 'wps_webinar', posts_per_page: -1, orderby: 'date_of_webinar', order: 'DESC' } %}
I have a custom field called "date_of_webinar" that I'm trying to order the results by but it appears to constantly be defaulting to the post publish date. Any help or am I just missing something simple?
Do I need to orderby the meta_value and then pass a meta_query array into the set args statement... and does TWIG markup support arrays inside that line?
June 28, 2021 at 7:21 PM #29175wpstudio
ParticipantSOLVED
Never mind, it just required me to pass the
meta_key
into the argument. For those who run into the same issue:{% set args = { post_type: 'wps_webinar', posts_per_page: -1, orderby: 'date_of_webinar', order: 'DESC', meta_key: 'date_of_webinar' } %}
July 23, 2021 at 11:15 PM #29674JonS
ParticipantThis looks like exactly what I need. Where did you put that line of code?
July 26, 2021 at 9:08 AM #29687Long Nguyen
ModeratorHi Jon,
You can follow this documentation to know how to use the custom query in View https://docs.metabox.io/extensions/mb-views/#running-php-functions
July 26, 2021 at 8:14 PM #29696JonS
ParticipantThanks for your reply. I've followed all the information but it just isn't making any difference to the order on on the page.
This is the code in my MB View which is set as a Shortcode type and I'm putting that shortcode into the archive template:
{% set args = { post_type: 'fixture', posts_per_page: -1, orderby: 'fixture_date_and_time', order: 'ASC', meta_key: 'fixture_date_and_time' } %} {% set posts = mb.get_posts( args ) %} <table> <thead align="left"> <th>Event</th> <th>Date / Time</th> <th>Venue</th> <th>Age Group</th> </thead> {% for post in query.posts %} <tr> <td><a href="{{ post.url }}">{{ post.title }}</a></td> <td><a href="{{ post.url }}">{{ post.fixture_date_and_time | date( 'D M j, Y h:i A' ) }}</a></td> <td>{{ mb.get_the_term_list( post.ID, 'fixture-venue', '', ', ' ) }}</td> <td>{{ mb.get_the_term_list( post.ID, 'fixture-age-group', '', ', ' ) }}</td> </tr> {% endfor %} </table>
I'm changing the order from ASC to DESC and it's making no difference.
What am I doing wrong?
July 26, 2021 at 8:36 PM #29697JonS
ParticipantFollowing up on this, I've also put the shortcode onto another page and it's just showing the data from that page, not the custom post 'fixture' so the code seems to be doing nothing.
July 26, 2021 at 9:32 PM #29700Long Nguyen
ModeratorHi Jon,
The
for
loop should iterate through theposts
variable, usemy_posts
to understand easier{% set my_posts = mb.get_posts( args ) %} {% for post in my_posts %}
July 26, 2021 at 10:17 PM #29702JonS
ParticipantBrilliant. That makes perfect sense. It's now working as it should except for one thing.
The {{ post.title }} is blank. The other fields are displaying perfectly. I would have thought the title was the easiest part so am somewhat confused!
July 26, 2021 at 10:28 PM #29703JonS
ParticipantMy view is now like this:
{% set args = { post_type: 'fixture', posts_per_page: -1, orderby: 'fixture_date_and_time', order: 'ASC' } %} <table> <thead align="left"> <th>Event</th> <th>Date / Time</th> <th>Venue</th> <th>Age Group</th> </thead> {% set my_posts = mb.get_posts( args ) %} {% for post in my_posts %} <tr> <td><a href="{{ post.url }}">{{ post.title }}</a></td> <td><a href="{{ post.url }}">{{ post.fixture_date_and_time | date( 'D j M, Y \\a\\t h:i A' ) }}</a></td> <td>{{ mb.get_the_term_list( post.ID, 'fixture-venue', '', ', ' ) }}</td> <td>{{ mb.get_the_term_list( post.ID, 'fixture-age-group', '', ', ' ) }}</td> </tr> {% endfor %} </table>
The URL isn't working, either, so it seems to be the built-in WP fields that don't work but the MB custom fields do.
July 27, 2021 at 12:45 AM #29707JonS
ParticipantI'm almost there. The title should be referenced with
{{ post.post_title }}
but I don't know what the url should be.July 27, 2021 at 12:49 AM #29708JonS
ParticipantSorted it. The url should be
{{ post.post_name }}
. -
AuthorPosts
- You must be logged in to reply to this topic.