I have written a custom sql query for our WordPress website (fairly elaborate and not achievable with the standard wordpress code) and I've got it to run via $pageposts = $wpdb->get_results($query, OBJECT);
, it yields the results I expect. Now I would like to integrate this query via a hook (ideally), so that I don't have to update the theme page so that we can still update the theme. I know that the hook pre_get_posts()
allows you to adjust the main query, but from what I've found it seems to only allow you adjust. E.g. $query->set( 'year', $today['year'] );
. I however, want to replace the query completely.
Is this doable using a hook so that I can utilize the main theme code using a hook from my child theme code?
Or will I need to clone the full page from the main theme to the child theme and use the code like this:
<?php
$pageposts = $wpdb->get_results($q, OBJECT);
if ($pageposts):
global $post;
foreach ($pageposts as $post):
setup_postdata($post);
?>