How to return only 3 posts in a view?

Support MB Views How to return only 3 posts in a view?Resolved

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #39955
    DeveloperDeveloper
    Participant

    I have a custom post type named event. At the moment it has about 20 posts.

    On the main events page the included MB view below displays all of the posts, chronologically ordered by the start date of the events.

    I would like to show only the first 3 event posts on the homepage, but limiting the posts_per_page to 3 shows no results. If I increase that value to 10, for example, I do get posts to display, but they aren't the next upcoming events.

    Is it possible to only display the next (chronologically speaking) 3 upcoming events?

    {% set args = {  post_type: "event", posts_per_page: 3 } %}
    {% set posts = mb.get_posts( args ) %}
    {% for key, post in posts|sort((a, b) => a.bdr_event_start_date <=> b.bdr_event_start_date) %}
    {% if post.bdr_event_start_date >= "now"|date("Y-m-d") or post.bdr_event_end_date >= "now"|date("Y-m-d") %}
    	<div class="bxb-event-grid-item">
    		<div>
    			<img src="{{ post.thumbnail.large.url }}" alt="{{ post.thumbnail.large.alt }}" width="{{ post.thumbnail.large.width }}" height="{{ post.thumbnail.large.height }}">
    			<a href="{{ post.url }}"><h2>{{ post.post_title}}</h2></a>
    			<div class="bxb-event-date-range">
    				{% if post.bdr_event_start_date == post.bdr_event_end_date %}
    					{{ post.bdr_event_start_date | date( 'F jS, Y' ) }}
    				{% endif %}
    				{% if post.bdr_event_start_date != post.bdr_event_end_date %}
    					{{ post.bdr_event_start_date | date( 'F jS' ) }}—{{ post.bdr_event_end_date | date( 'F jS, Y' ) }}
    				{% endif %}
    			</div>
    		</div>
    	</div>
    {% endif %}
    {% endfor %}

    Thank you for any assistance you can provide!

    #39993
    PeterPeter
    Moderator

    Hello,

    Support a customization code is beyond our scope of support, please read more here https://support.metabox.io/topic/support-policy/

    If you cannot accomplish the job, I recommend contacting an expert developer to help you with this or create a customization request here https://metabox.io/contact/
    Our developer will help you with their best.

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