Order with custom table

Support MB Views Order with custom table

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #43492
    Fabio ValenteFabio Valente
    Participant

    I'm not able to order a view using a custom table field (data_ora_inizio).
    This is the code, any ideas?

    {% set args = { post_type: 'evento', posts_per_page:10, orderby: 'data_ora_inizio', order: 'DESC'} %}
    {% set posts = mb.get_posts( args ) %}
    <main class="max-x-3xl mx-auto p-4">
        {% for post in posts %}
            <div class="flex mb-6 border border-gray-400 rounded">
                <div class="flex-1 p-4">
                    <h3><a href="{{ post.url }}"> {{ post.title }}</a></h3>
                    <em>{{ post.ID }} --- {{ post.data_ora_inizio | date( 'd/m/Y' ) }} - {{ post.citta.label }}</em>
                    <div class = "mt-3">
    					{{ post.indirizzo }}<br><br>
                    </div>
                </div>
            </div>
        {% endfor %}
        
        {{ mb.get_the_posts_pagination() }}
    </main>
    #43502
    PeterPeter
    Moderator

    Hello Fabio,

    If the custom field "data_ora_inizio" saved value in a custom table, it isn't possible to order or get posts by that field value.

    To get posts by custom fields in the custom table, you need to make an extra query to get the post IDs first. Then use these IDs to get full post objects.
    Or get field value by a given post ID.

    Read more in the documentation
    https://docs.metabox.io/extensions/mb-custom-table/#query-posts-with-wp_query
    https://docs.metabox.io/extensions/mb-custom-table/#getting-field-value

    #43506
    Fabio ValenteFabio Valente
    Participant

    Hi Peter, thank you for your quick response.
    My field group are in a custom table, fortunately I'm in development yet, so I can change.
    Is it possible to change the Field Group settings from custom table to WP, unselecting 'Save data in a custom table'?
    Are there problems?
    Thx

    #43512
    PeterPeter
    Moderator

    Hello,

    Yes, it is possible. But you need to add the field value for each post manually again because the value is saved in a different table.

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