Support Forum
In 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?
SOLVED
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' } %}
This looks like exactly what I need. Where did you put that line of code?
Hi 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
Thanks 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?
Following 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.
Hi Jon,
The for
loop should iterate through the posts
variable, use my_posts
to understand easier
{% set my_posts = mb.get_posts( args ) %}
{% for post in my_posts %}
Brilliant. 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!
My 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.
I'm almost there. The title should be referenced with {{ post.post_title }}
but I don't know what the url should be.
Sorted it. The url should be {{ post.post_name }}
.