Skip Navigation

[Resolved] View doesn't work with custom post_type WP_Query

This support ticket is created 3 years, 11 months 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by Jamal 3 years, 11 months ago.

Assisted by: Jamal.

Author
Posts
#1994529
Screenshot 2021-03-21 at 12.13.46 AM.png

Dear Sir/Madam

I have a page with a View inside, I have custom shortcode in View

<wpv-loop>
Post ID: [wpv-post-id] 
	<div class="row">
		<div class="col-md-4">
			<div class="image-container" style='background-image: url([get_tour_featured_image id="[types field='application-tour-number'][/types]"])'></div> 
		</div>
		<div class="col-md-8">
			<div class="caption">
				<h1 class="title"><span class="tb-uppercase-override">[get_tour_code id="[types field='application-tour-number'][/types]"]</span> [get_tour_title id="[types field='application-tour-number'][/types]"] </h1>
				~[types field='application-tour-number'][/types]~
				[types field='application-departure-date' style='text' format='d/m/y'][/types]
			</div>
		</div>
	</div>
</wpv-loop>

Is there any limitation to the shortcode if doing WP_Query with toolset custom post type, I need to query custom post type "Tour" by getting the post id from my another custom post type "Application". I show [wpv-post-id] within the loop but it always returns the post id of the page instead of my custom post Application post id.

Below please find the code how I do the WP_Query()

add_shortcode('get_tour_featured_image', 'fn_get_the_post_thumbnail_url');
function fn_get_the_post_thumbnail_url( $atts ) {
    $query = new WP_Query( array( 
        'ID' => $atts['id'],
        'post_status'   => 'publish',
        'post_type'     => 'tour',
        'posts_per_page' => 1
    ));
    
    if ( $query->have_posts() ) {
        $query->the_post();
        $u = get_the_post_thumbnail_url();
        wp_reset_postdata();
        return $u;
    }
}

Please advise whether WP_Query has limitation to custom post type query.

#1994561

Hello and thank you for contacting the Toolset support.

I think that the custom code changes the global $post variable. I would suggest to adjust the code to not alter it at all. For example, use get_posts function instead of a WP_Query instance.
Then pass the post_id to get_the_post_thumbnail_url without altering and relying on the global $post.
Something like:

add_shortcode('get_tour_featured_image', 'fn_get_the_post_thumbnail_url');
function fn_get_the_post_thumbnail_url( $atts ) {
    $posts= get_posts( array( 
        'ID' => $atts['id'],
        'post_status'   => 'publish',
        'post_type'     => 'tour',
        'posts_per_page' => 1
    ));
     
    if ( count($posts) ) {
        $u = get_the_post_thumbnail_url(posts[0]->ID);
        return $u;
    }
}

Does it make sense?
This being, I would like to note that this is custom code and therefore out of the scope of the support forum. https://toolset.com/toolset-support-policy/

#1994745

Dear Jamal,

Many thanks for your support. You are nice to help me to solve the problem. I understand custom code is not the scope of the support but I still have a question why the WP_Query() doesn't work in the Toolset View loop?

Toolset View loop will loop the post and then I pass the looped post id to the shortcode to query another custom post's content by the secondary query. I do call wp_reset_postdata() to reset the post data, then the global $post variable should be restored to the current post and continue the loop.

Is there any special declaration I need to do when I do the secondary query using WP_Query()?

Best regards,

Kelvin.

#1995131

Hello Kelvin and thank you for your feedback. However, answering your question is, somehow, custom code support 🙂

The issue is probably arise when using "$query->the_post();" which alters the global $post variable. If you can provide a clear bug description, I'll be more than happy to escalate it to our developers. But, if you are asking me to debug why this code is not working as you would expect, it goes to the out-of-scope part of my day.

A bug description would be something like:
1. I go to xxx
2. I do xxx
3. I expect to have xxx
4. Instead, I get xxx
You can use the link below to login to one of our test servers and reproduce the issue on it. This way, we can exclude any issue that can come from a conflict with 3rd party component, or a server-related issue.
hidden link

#2005119

Dear Jamal,

I think my issue will only be happened on my custom post, please advise how I can export/import my custom post to the site your suggested.

Best regards

Kelvin

#2005199

Hello Kelvin,

I am not sure to understand your last request. Are you looking to export this tour CPT and import it in a Toolset CPT?
Before answering that, I would like to make sure that I understand your original request and help you with it.

Inside a Toolset view or content template, the global $post object is changed by Toolset. Inside a view, the global $post is the current post in the loop. Once the view is rendered, Toolset restores the original global $post. Toolset restores in someway the main query(WP_Query instance). I would prefer not to change the global $post inside a content template or a view.

Let's get back to the loop of your view. You are calling the custom shortcode "get_tour_featured_image" with the value of "application-tour-number" to get the feature image of a tour CPT that has the same ID as the "application-tour-number", right?
Why not using get_post to get the tour object directly, without using a WP_Query instance or get_posts.
https://developer.wordpress.org/reference/functions/get_post/

You can also just use Toolset shortcodes directly, passing the tour number in the item attribute. Check this article https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/

[wpv-post-featured-image output="url" item="[types field='application-tour-number'][/types]"]