Skip Navigation

[Resolved] Run custom SQL Query and attach or pass to View

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

Problem:

I would like to know if it is possible to create my own mysql query to query posts and postsmeta tables and generate the proper output.

Solution:

I suggest you try with filter hook wpv_filter_query:

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

For example,

1) Setup a post view, query posts, without any filter.

2) use "wpv_filter_query" to trigger a custom PHP function, in this PHP function, use your custom SQL query to get those specific post IDs, then pass the value as parameter "post__in" of query

Relevant Documentation:

https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters

100% of people find this useful.

This support ticket is created 4 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: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by Mukesh 4 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#1500757

Tell us what you are trying to do?
I would like to know if it is possible to create my own mysql query to query posts and postsmeta tables and generate the proper output. I would like to then pass the results to the View and use the View editors (Loop Editor, Content Template editor) etc that I would normally use with the Views feature.
Is there any documentation that you are following?
I found a couple of posts on the Toolset Support section but not sure if that is what would work with what I want to do.
1. https://toolset.com/forums/topic/using-posts_join-and-posts_where-to-query-posts-postmeta-with-another-table/
2. https://toolset.com/forums/topic/getting-the-right-query-for-toolset-created-tables/
Is there a similar example that we can see?
Not that I know of.
What is the link to your site?
lopdev.peoplefirstpharmacy.com BUt I will need to grant you access as admin or give you a backup.

#1501325

Hello,

I suggest you try with filter hook wpv_filter_query:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

For example,
1) Setup a post view, query posts, without any filter.
2) use "wpv_filter_query" to trigger a custom PHP function, in this PHP function, use your custom SQL query to get those specific post IDs, then pass the value as parameter "post__in" of query:
https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters
post__in (array) – use post ids.

#1502315

Hi Luo,
Thanks for the suggestion. I haven't tried it yet, but seems like it would work. However I have one item to clarify, in your step #2, you mention pass the value of Post IDs as parameter "post__in". How would I do that in the wpv_filter_query? I understand if I was creating a new WP_Query object but not sure how to use it in wpv_filter_query.

Thanks,
Mukesh

#1502535

Dear Mukesh,

As I mentioned above, you can use your custom SQL query to get the post IDs as an array, for example:
$post_ids = array(123, 456);
Then pass the IDs value as parameter "post__in", for example:
$query_args['post__in'] = $post_ids;

#1505447

Thanks Luo. That makes sense. I will try this out! I'm going to resolve this for now and may open if I have additional queries.