Skip Navigation

[Gelöst] Filter View by Post ID of Current Post.. HOW?!?!

This support ticket is created vor 6 Jahre, 6 Monate. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 Antworten, has 2 Stimmen.

Last updated by Christian Cox vor 6 Jahre, 6 Monate.

Assisted by: Christian Cox.

Author
Artikel
#578646

Hello,

I've created custom fields for a custom post type called Events(Events Manager plugin).

I want to display these custom fields using a View as Views give me more flexibility such as Conditional sections.

To do this, I need to pass the current post's Post ID to the View so that it can filter and display the custom fields that belong to the current post. I can't figure out how to do this.

The 3 options provided when filtering a View by Post ID are:

"Posts with those IDs" - CAN'T USE because I don't know the Post IDs beforehand and they are always changing.
"Posts with IDs set by this URL parameter" - CAN'T USE because the Post ID isn't in a URL parameter.
"Posts with IDs set by the View shortcode attribute" - CAN'T USE because there doesn't seem to be a way to dynamically set this attribute to the current post's Post ID.

Why is there no simple "ID of Current Post" option?!?!?!?!

Urgent... How can I filter a View based on the current post's Post ID?

Thanks.

#578653

I think I found a temporary solution but there must be a more elegant way of achieving this... such as an option to filter based on the current post's Post ID.

My solution for now is to create a shortcode that recreates the View shortcode with the current post's Post ID in it:

/* Display event-files View for Current Post ID */
function sdcc_event_files() {
	global $post;
	$event_files_shortcode = "[wpv-view name=\"event-files\" ids=".$post->ID."]";
	return do_shortcode($event_files_shortcode);
}
add_shortcode( 'event_files', 'sdcc_event_files' );
#578794

"Posts with IDs set by the View shortcode attribute" - CAN'T USE because there doesn't seem to be a way to dynamically set this attribute to the current post's Post ID.
Hi, you shouldn't need a custom shortcode for this. If you want to access the ID of the current Post (i.e. the current page) you can use the $current_page operator in the id attribute of your post ID shortcode:

[wpv-post-id id='$current_page']

This will always give you the ID of the current page, rather than the ID of the current post in a Loop. So you could pass that into a shortcode attribute for your View as an ID filter:

[wpv-view name="your-view-slug" ids="[wpv-post-id id='$current_page']"]

Let me know if this doesn't resolve your issue.

#578812

Thanks for the reply. Your example does not seem to work...

[wpv-view name="your-view-slug" ids="[wpv-post-id id='$current_page']"]

Using the above, with my own view slug, produces no results. All that is displayed on the page is...

"]

This is what I had experienced previously when trying different ways of injecting the current post's ID into the Views shortcode. My understanding is that you can't use one shortcode inside another shortcode like you've done in your example.

#578816

Well that's silly. The second code block in my previous post doesn't display the code I placed into it. Anyway, that code, which appears as text on my web page when using the example shortcode you provided is...

"]

... the ending double-quote and square bracket of a shortcode.

#578819

My understanding is that you can't use one shortcode inside another shortcode like you've done in your example.
Sure, you can nest shortcodes. I suspect something else is going on. Can you share a screenshot showing the settings for your Query Filter, as well as any code used in your Loop Output editor and any Content Templates used for this View?

Then, please share how you have added this View to the page. Is it nested inside another View, or placed directly in the page content, or placed in a Content Template, etc? Is it part of a WordPress Archive or shown on a custom post? Why is it necessary to pass the current post's ID into the filter at all - getting information from the current post is the default behavior of a View, so you shouldn't have to supply an ID at all unless the View is placed in another context.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.