Skip Navigation

[Resolved] Query Filter to Display Only Current Post Data in View

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to use the value of a custom field on the current page to determine where to place some content in my content template.

Solution: Ensure your View is set to include the current page in results, and that your custom field values match your filter settings exactly.

This support ticket is created 7 years ago. 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.

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 6 replies, has 2 voices.

Last updated by davidT-17 6 years, 12 months ago.

Assisted by: Christian Cox.

Author
Posts
#513516
PC-Project-Image-3.jpg
PC-Project-Image-2.jpg
PC-Project-Image-1.jpg

I have two views in a custom post type (Vehicle) content template which have query filters to display information submitted via that CPT's CRED form based on a specific custom field's value. If the value of the custom field is "A" I want the information to show up in one View ("View A"), but if the value of the custom field is "B" I want the information to show up in the other View ("View B").

I just want the information from the CPT (Vehicle) that the views are in, so I tried the following as suggested by Nigel in this forum post (https://toolset.com/forums/topic/i-cant-filter-view-to-current-post/):
"...You can just add directly to the widget (using the id="$current-page" attribute with each shortcode).

If there is some valid reason for making a view with just one result which is the currently displayed post then where you insert the view you will need to pass the id of the current page as a shortcode attribute and then add a query filter for the post id to your view that takes the value from that shortcode attribute.

You have already linked to the documentation on how to do that in your original post.

Where you insert the view in your widget you add a custom attribute, something like this:

[wpv-view name="list-posts" ids="[wpv-post-id id='$current-page']"]
And in the view itself add a query filter for post id that takes its value from this shortcode attribute 'ids'."

I added a query filter to each of the views and had each filter by post id. Then I set each query filter to filter posts to include Posts with IDs set by the View shortcode attribute: ids. After saving I went to the content template for Vehicles and modified this view shortcode: [wpv-view name="view-to-display-vehicle-relationship-of-original-vehicle-cpt-author" limit="-1"] to include Nigel's shortcode attribute resulting in this: [wpv-view name="view-to-display-vehicle-relationship-of-original-vehicle-cpt-author" limit="-1" ids="[wpv-post-id id='$current-page']"]

The above results in "no posts found." I have also tried inserting id="$current-page" directly into the views shortcodes [wpv-view name="view-to-display-vehicle-relationship-of-original-vehicle-cpt-author" limit="-1" id="$current-page"] with the same result of "no posts found."

#513783

Hi, I'll be glad to take a look. First, can you enable Toolset's Views debug tool? You can see information about that here (scroll down to "The Views debug tool"):
https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/

Once that is enabled, please reload the page and allow pop-ups, then refresh the page again. You should see a pop-up window appear that contains some configuration information about each View on the page. Please open all the content areas, then copy and paste the content you find.

#514090

Page info

Current page hidden link
Total memory used 6.1104MB
Render time 2.62
Total MySQL queries 210
CPU usage 16.51%
Elements info

- Vehicle Admin Info

ID 1907
Name Vehicle Admin Info (Edit)
Kind of element Content Template
Render time 0.02598
Memory used 0.0354MB
+ Template results

- Current post body

Kind of element Post body
Render time 0.0064700000000002
Memory used 0.007MB
+ Post body data

- Fourth Level View of the Vehicle Records (Vehicle Records ordered by post title, descending)

ID 2321
Name Fourth Level View of the Vehicle Records (Edit)
Kind of element View
Render time 0.04855
Memory used 0.0054MB
Summary Vehicle Records ordered by post title, descending
Query args Show
MySQL query Show
Items found 0
Query results Show
Filters
Filter arguments before the query using wpv_filter_query Show
Filter the returned query using wpv_filter_query_post_process Show
- Child View for Listing Vehicle Specific Gen Images (Vehicle Gen Images ordered by post date, descending)

ID 2196
Name Child View for Listing Vehicle Specific Gen Images (Edit)
Kind of element View
Render time 0.03908
Memory used 0.0009MB
Summary Vehicle Gen Images ordered by post date, descending
Query args Show
MySQL query Show
Items found 0
Query results Show
Filters
Filter arguments before the query using wpv_filter_query Show
Filter the returned query using wpv_filter_query_post_process Show
- Child View for Listing Vehicle Record Images in Vehicle Grandparent Post (Vehicle Records ordered by post date, descending)

ID 2164
Name Child View for Listing Vehicle Record Images in Vehicle Grandparent Post (Edit)
Kind of element View
Render time 0.03916
Memory used 0.001MB
Summary Vehicle Records ordered by post date, descending
Query args Show
MySQL query Show
Items found 0
Query results Show
Filters
Filter arguments before the query using wpv_filter_query Show
Filter the returned query using wpv_filter_query_post_process Show
- Child View for Listing Vehicle Specific Vehicle Videos (Vehicle Videos ordered by post date, descending)

ID 1453
Name Child View for Listing Vehicle Specific Vehicle Videos (Edit)
Kind of element View
Render time 0.04433
Memory used 0.001MB
Summary Vehicle Videos ordered by post date, descending
Query args Show
MySQL query Show
Items found 0
Query results Show
Filters
Filter arguments before the query using wpv_filter_query Show
Filter the returned query using wpv_filter_query_post_process Show
- View to Display Vehicle Relationship of Original Vehicle CPT Author (Vehicles ordered by post date, descending)

ID 2404
Name View to Display Vehicle Relationship of Original Vehicle CPT Author (Edit)
Kind of element View
Render time 0.05035
Memory used 0.0042MB
Summary Vehicles ordered by post date, descending
Query args Show
MySQL query Show
Items found 0
Query results Show
Filters
Filter arguments before the query using wpv_filter_query Show
Filter the returned query using wpv_filter_query_post_process Show
- Child View for Vehicle Ownership Histories (Owner Histories ordered by Field - wpcf-sale-year, descending)

ID 2379
Name Child View for Vehicle Ownership Histories (Edit)
Kind of element View
Render time 0.10916
Memory used 0.0188MB
Summary Owner Histories ordered by Field - wpcf-sale-year, descending
Query args Show
MySQL query Show
Items found 2
Query results Show
Filters
Filter arguments before the query using wpv_filter_query Show
Filter the returned query using wpv_filter_query_post_process Show
+ View Loop

- View to Display Vehicle Relationship of Original Vehicle CPT Author Not Owner (Vehicles ordered by post date, descending)

ID 2406
Name View to Display Vehicle Relationship of Original Vehicle CPT Author Not Owner (Edit)
Kind of element View
Render time 0.04147
Memory used 0.0009MB
Summary Vehicles ordered by post date, descending
Query args Show
MySQL query Show
Items found 0
Query results Show
Filters
Filter arguments before the query using wpv_filter_query Show
Filter the returned query using wpv_filter_query_post_process Show
- Child View for Vehicle Service Histories (Service Histories ordered by post author, descending)

ID 2381
Name Child View for Vehicle Service Histories (Edit)
Kind of element View
Render time 0.05444
Memory used 0.0037MB
Summary Service Histories ordered by post author, descending
Query args Show
MySQL query Show
Items found 1
Query results Show
Filters
Filter arguments before the query using wpv_filter_query Show
Filter the returned query using wpv_filter_query_post_process Show
+ View Loop

#514169

Okay unfortunately I don't see anything obvious in the debug output. It might be easiest for me to take a look at your wp-admin area to see some of your configurations and post type organization. If I need to make changes, I would like to clone your site using the Duplicator plugin so I can install it locally. This way I won't modify your live site when I make adjustments. If that's okay with you, please provide the site login credentials in the private reply fields here.

#514942

I'm sorry, the WordPress login password doesn't work for me. Could you double check the credentials? Also if there are any IP protections enabled, I can provide that info for you.

#515056

Ok thanks. I see a couple of things going on here:

1. In your "View to Display Vehicle Relationship of Original Vehicle CPT Author" View here: /wp-admin/admin.php?page=views-editor&view_id=2404, you have checked "Don't include current page in query result." Since you want to include information from the Vehicle on the current page, this should be unchecked. The same goes for the corresponding Author Not Owner View.

2. In your Vehicle Relationship Custom Field filter, you have set the filter to look for the strings "current owner" and "previous owner" but the possible values are "Current Owner" and "Previous Owner" as defined in your Vehicle Relationship custom field options here: hidden link
The strings must match, and the filter will not be lenient on capitalization differences.

Please make these modifications and check that you can now see the results you expect for each Vehicle. Then if you need assistance determining whether to show information in "A" or "B" as you were describing earlier, I'll be glad to help.

#517614

Thank you Christian, that fixed everything perfectly.

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