Skip Navigation

[Resolved] how to stuff parametric search results into a query array for use elsewhere

This support ticket is created 10 years, 3 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.

This topic contains 11 replies, has 3 voices.

Last updated by Stephen 10 years, 2 months ago.

Assisted by: Bigul.

Author
Posts
#136118

I have created a parametric search which returns 10 results per page. I have in my template a completely separate query where I would like to use the ids or titles of the parametric search result to feed into my other query. Is this possible? I am not sure how to capture those values...

#136189

I get the feeling I need something like what is referred to here:

https://toolset.com/documentation/user-guides/views-api/

BUT, there is no mention how to integrate a parametric search and account for paging...

#136373

Bigul
Supporter

Dear Stephen,

Sorry render_view will not take Custom Parameters, it only takes the name, title or id of the View.

--
With Regards

Bigul

#136445

I don't think you are understanding me, I don't want to pass something TO the view, I want to reuse the query FROM the view once it has run. Fro example, in the parametric search I return a list of titles of the posts queried. I would like to be able to call those again in my template to use in a separate custom query. Ideally I would be able to save those posts titles or ids into an array, and use that in my other query. Without that, I have to write two separate queries and the performance is terrible (and a waste, since the data I need is already there in the query the parametric search performs.

#136926

Bigul
Supporter

Dear Stephen,

Sorry, it is not possible, I confirmed it with our developers. It will be very to had to achieve.


With Regards

Bigul

#137873

ok, but that makes my web app highly inefficient and slow. There should be a way to hook into the query that is output through views...

#138838

Juan
Supporter

Timezone: Europe/Madrid (GMT+01:00)

Hi Stehen.

I'm on the ToolSet dev team, and I work on Views. I think that we can find a way to do what you want, not a direct way but I'm sure we will get there 🙂

In Views we have a filter that gets executed just after the WordPress query has been run: wpv_filter_query_post_process. It takes as parameters the query itself and the View settings, and needs to return the query. You can read the documentation about it here:
https://toolset.com/documentation/user-guides/views-filters/

As this is a filter on the already run query, it needs to return the query itself, so there is no direct way to "take out" information from that query and pass it to your template for you to use it on your other query. My idea consists on storing the relevant data (for example, an array of post IDs) that you need in a global variable that would be there accessed in your template. I do not like abusing global variables, but in this case it might be useful for you. So let's start.

First, you need to add the filter:

add_filter('wpv_filter_query_post_process', 'modify_empty_query', 10, 2);
function modify_empty_query($query, $view_settings) {
	global $my_data;
	$my_data = array();
	if ( is_page('265') ) {
		
		foreach ($query->posts as $mypost ) {
			$my_data[] = $mypost->ID;
		}
	}
	return $query;
}

Note that I have added a condition to populate the $my_data array: to be on page with ID 256. This page is the one I'm using to display the results of the View query. You should adjust your condition to your needs using WordPress conditional tags, so that global $my_data only gets populated in the scenario that you need it to.

After that, just open your template and access the array like so:

global $my_data;

Keep in mind that $my_data will only hold the IDs of the posts returned by the query if you access it after the query has been output in the WordPress loop.

Hope it helps. Let me know if this solves your issue.

Regards,
Juan de Paco

#138855

Fantastic, I will try this out later today and let you know, thanks very much!

#139549

Bigul
Supporter

Dear Stephen,

Hope it is working for you. Please let us know the feedback when you are free.


With Regards

Bigul

#140014

Well, I added that to my functions file, and changed the page id to 166 (my results page) and then added the following to my template to test:

global $my_data;
 var_dump($my_data);

but it returns a NULL result.

Any ideas?

#140090

Juan
Supporter

Timezone: Europe/Madrid (GMT+01:00)

Hi Stephen.

I just tested again,and it's working fine on my side. Could you please check that:
* Your template is being loaded.
* You call global $my_data after the WordPress loop has been run on that page. It has to be after the call to the_content() for that page, since the View is output (and its query is run) in that moment.

If you can not make it work, we will check your template and try to find the reason.

Regards,
Juan de Paco

#140100

That was it! It was not after the loop. Once I moved it there, it worked. Thanks!!

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