How to order/sort related posts by title or custom field value?

Support MB Views How to order/sort related posts by title or custom field value?

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #36452
    Brent WilsonBrent Wilson
    Participant

    How can I order posts in MB views based on title or custom field value? Ascending or descending, etc.?

    For example, with the following MB views template code:

    <h2>
    	Presenters
    </h2>
    <div class="presenters_container">
    {% for post in relationships.events_presenters_relationship.to %}
    	<a href="{{ post.url }}">
    		<span class="presenter_card">
    			<img src="{{ post.thumbnail.medium_large.url }}" width="{{ post.thumbnail.medium_large.width }}" height="{{ post.thumbnail.medium_large.height }}" alt="{{ post.thumbnail.medium_large.alt }}">
    			<h3>
    				{{ post.title }}
    			</h3>
    		</span>
    	</a>
    {% endfor %}

    Please let me know some specific examples of what I need to add to the code and where I need to add it, as I am not fluent with php.

    #36469
    Long NguyenLong Nguyen
    Moderator

    Hi Brent,

    Basic knowledge about WordPress query and PHP code is needed to create the template in MB Views. Following your previous topic, you can create the custom query with the order argument like this

    <h2>
    	Presenters
    </h2>
    {% set args = { post_type: 'presenters', nopaging: true, order: 'DESC', orderby: 'title', relationship: { id: 'events_presenters_relationship', from: post.ID } } %}
    {% set presenters = mb.get_posts( args ) %}
    ...

    Read more on the documentation https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
    https://docs.metabox.io/extensions/mb-views/#custom-query

    #36655
    hartsook@gmail.com[email protected]
    Participant

    Is it possible to add the order parameter to the relationship shortcode e.g. [mb_relationships id="book-to-series" direction="to" mode="ul" order="ASC"]?

    What is the default order to the related posts, it doesn't seem to be post publication date or Post Title?

    #36660
    Long NguyenLong Nguyen
    Moderator

    Hi hartsook,

    1. No, it is not possible to add the order parameter to the relationship shortcode. You can find the accepted attributes here https://docs.metabox.io/extensions/mb-relationships/#shortcode

    2. The default order, orderby values are inherited from the WordPress settings. Please read more here https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

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