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...
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.
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.
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.