Custom Fields
Re-Envisioned
Support MB Views Ability to ORDERBY a custom field in a View?

  • Creator
    Topic
  • #29174
    Resolved wpstudio
    Participant

    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?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Author
    Replies
  • #29175
    wpstudio
    Participant

    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' } %}

    #29674
    JonS
    Participant

    This looks like exactly what I need. Where did you put that line of code?

    #29687
    Long Nguyen
    Moderator

    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

    #29696
    JonS
    Participant

    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?

    #29697
    JonS
    Participant

    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.

    #29700
    Long Nguyen
    Moderator

    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 %}
    #29702
    JonS
    Participant

    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!

    #29703
    JonS
    Participant

    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.

    #29707
    JonS
    Participant

    I'm almost there. The title should be referenced with {{ post.post_title }} but I don't know what the url should be.

    #29708
    JonS
    Participant

    Sorted it. The url should be {{ post.post_name }}.

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.