Exclude specific posts from MB View and Ordering

Support MB Views Exclude specific posts from MB View and Ordering

  • This topic has 6 replies, 2 voices, and was last updated 2 years ago by KaraKara.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #41252
    KaraKara
    Participant

    Hi there - I have a MetaBox View set up via shortcode. I have a CPT: Speaker and another CPT: Topic. A relationship is set up to connect them.
    In some cases, I have a speaker with up to 5 related topics. I don't want to sever the relationship as I need it elsewhere on the site.

    --> Is it possible to exclude by post id, perhaps for this specific view?

    --> Also, is it possible to define how they're ordered? I'd like to do it by date added in desc order.

    Here's my code:

    {% set relationship = attribute( relationships, 'speaker-topic' ) %}
    {% for post in relationship.from %}
    <div class="speaker-topic">
    <div class="live-date">{{ post.date_of_topic.value }} at {{ post.start_time | date( 'h:i A' ) }}</div>
    <div class="topic-desc"><h2>{{ post.title }}</h2>
    {% set field = attribute( post, '1-2_sentence_description' ) %}
    <p>{{ field }}<p></div>
    <div class="elementor-button topic"><a href="{{ post.url }}">More on this topic</a></div>
    </div>
    {% endfor %}

    Thank you!

    #41258
    PeterPeter
    Moderator

    Hello,

    1. Is it possible to exclude by post id, perhaps for this specific view?
    You can use the function get_queried_object_id() to get the current post/page ID and create an if statement to run the code or not based on the condition.
    https://developer.wordpress.org/reference/functions/get_queried_object_id/

    2. Is it possible to define how they're ordered? I'd like to do it by date added in desc order.
    You can use the function get_posts() and add the relationship to the arguments to order the post.
    Please read more on the documentation https://docs.metabox.io/extensions/mb-views/#custom-query
    and refer to this topic
    https://support.metabox.io/topic/how-to-order-sort-related-posts-by-title-or-custom-field-value/?swcfpc=1#post-36469

    #41263
    KaraKara
    Participant

    Hi Peter -
    Thanks for getting back to me 🙂

    Let me clarify a bit more on my first question:

    The code above is for a view embedded via shortcode on all Speaker Pages.
    It shows the topics that speaker is assigned to.

    Some speakers are assigned to 5 topics, but I only want to display 2.
    -->How do I exclude those 3 other topics from the view?

    I don't want to remove the relationship between the Speaker and Topic for those other 3 because I rely on that relationship on other pages of the site.

    Thank you!

    #41269
    PeterPeter
    Moderator

    Hello,

    Yes, I understand that. Did you try to create an if statement to check the current post ID and run the code inside?

    For example:

    {% if mb.get_queried_object_id() == 123  %}
    {% set relationship = attribute( relationships, 'speaker-topic' ) %}
    ...
    {% endif %}
    #41319
    KaraKara
    Participant

    Hi Peter -

    Ah, I think I see what you're saying now. But, I don't know much PHP so no, I haven't tried to create the if statement you mentioned. Is this something you could you help me out with it?

    If so,
    The speaker page is id=692
    I want to exclude these speaker topics from the view: 895, 890, 882

    And then, do I add this to the View code I shared above or somewhere else?

    Thank you 🙂

    #41331
    PeterPeter
    Moderator

    Hello,

    You can try to use this View code to exclude the content from the provided post IDs

    {% set post_ids = [895, 890, 882] %}
    {% set current_post_id = mb.get_queried_object_id() %}
    {% if ( current_post_id not in post_ids ) %}
        <h1>heading here</h1>
        {% set relationship = attribute( relationships, 'speaker-topic' ) %}
        ...
    {% endif %}
    #41340
    KaraKara
    Participant

    Thanks so much for that, Peter!
    That works, except I need it the other way around.

    I think the way you wrote it excludes the view from showing up on those posts

    But, I need to exclude post ids [895, 890, 882] from showing up on post id [692].

    Can you help me with this?
    Thank you!

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