Skip Navigation

[Resolved] Reset query probelm when using shortcode

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

Problem:

The issue here is that the user wanted to query the Post Relationships.

Solution:

We actually have API functions for these and it should be able to assist you with achieving this.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

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

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Shane 6 years, 6 months ago.

Assisted by: Shane.

Author
Posts
#953540

I am trying to: get the result of connected posts images using shortcode. In it I used wp_query and at the end if I reset the query, the default query of view is also gets reset.

Link to a page where the issue can be seen: hidden link

I expected to see: the result of main query if I reset the wp_query in shortcode.

Instead, I got: If i use permalink after reset query in shortcode, it returns the bdx page link instead of the link of the page in the loop

#953716

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Will,

Thank you for contacting our support forum.

I believe this is because the reset query function resets all the wp_query.

Could you let me know the contents of the shortcode? because you may not need to query the database directly for the information since wordpress has many prebuilt functions.

Thanks,
Shane

#954400

Here is the shortcode code:

$results = new WP_Query(array( 
		'post_type' => 'commucommunity-media',
		'posts_per_page' => -1,
		'toolset_relationships' => array(
		'role' => 'child',
		'related_to' => $pid,
		'relationship' => 'community_commucommunity-media',
		),
		));
	if ( $results->have_posts() ) {
		while ( $results->have_posts() ) {
			$results->the_post();
			$image_url = get_post_meta( get_the_ID(), 'wpcf-slider-image', true );
			if ( $types ) {
				$output .= '$image_url';
			}
		}
	}
wp_reset_postdata();

Thanks

#954535

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi WIll,

Thank you for sharing this code.

However you use our function below to do this exact same thing.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Please try using this method and let me know if this helps.
Thanks,
Shane