I find myself in a strange situation where I need to add the argument "facetwp => true" to a WP_Query loop that returns the current post/page. However, in all of my research I cannot figure out a way to use arguments to return the current post/page. I found some references to using rewind_posts() in order to loop back and match the current ID, but it was all too technical and didn’t have examples.
I know that WordPress gets the content for the current page by default, but I have a unique setup where I’m relying on Oxygen Builder, ACF, and FacetWP with Latitude/Longitude fields in ACF (not the Google Maps field) in order to populate a Google Map on single custom post pages. I cannot get the Map Facet to identify the proper query on this page, so I am trying to add a custom code block with "facetwp => true" that returns the current page so that it recognizes the correct query.
For example:
<?php
$args = array(
'facetwp' => 'true'
// Additional arguments to return current post/page
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
}
}
wp_reset_postdata();
?>
I can find all kinds of ways to return other posts/pages using additional arguments, but no luck returning the current page. Can anyone help?!?
EDIT: Now that I re-read my question. It’s not that I need to use arguments to call the post/page, I just need the “facetwp => true” argument to be in the loop. I can rely on any other method to return the current post, it doesn’t have to be arguments that achieve this.
EDIT 2: More research. This seemed somewhat promising but isn’t working correctly. Anyone have any thoughts?
<?php
global $wp_query;
$current_post_id = $wp_query->post->ID;
echo "The current post ID is: " . $current_post_id;
$args = array(
'post_type' => 'court',
'posts_per_page' => 1,
'post_in' => $current_post_id
);
$related_posts = new WP_Query( $args );
while( $related_posts->have_posts() ):
$related_posts->the_post();
// The Loop
echo '<li>' . get_the_title() . '</li>';
endwhile;
?>
2
Answers
Your suggestion worked @EmielZuurbier!
I was able to get it working using the above code and the suggestions you made. Thank you so much!!!
With the
pre_get_posts
action hook you can modify the$query
object that is used for the primary post loop, like in your example, a singlecourt
page, before the query is actually made.In the example below the
$query
is modified by adding thefacetwp
property to the query if we’re on a single court page, using the main query, and not in the admin environment.This should allow you to use the primary post loop without having to create a custom query on your page.