Exclude specific posts from MB View and Ordering
- This topic has 6 replies, 2 voices, and was last updated 2 years ago by
Kara.
-
AuthorPosts
-
March 29, 2023 at 3:55 AM #41252
Kara
ParticipantHi 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!
March 29, 2023 at 6:48 PM #41258Peter
ModeratorHello,
1. Is it possible to exclude by post id, perhaps for this specific view?
You can use the functionget_queried_object_id()
to get the current post/page ID and create anif
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 functionget_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-36469March 29, 2023 at 10:31 PM #41263Kara
ParticipantHi 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!
March 30, 2023 at 8:24 PM #41269Peter
ModeratorHello,
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 %}
April 4, 2023 at 12:31 AM #41319Kara
ParticipantHi 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, 882And then, do I add this to the View code I shared above or somewhere else?
Thank you 🙂
April 4, 2023 at 5:56 PM #41331Peter
ModeratorHello,
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 %}
April 4, 2023 at 11:37 PM #41340Kara
ParticipantThanks 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! -
AuthorPosts
- You must be logged in to reply to this topic.