Basically, I’m trying to follow this tutorial to add an action to the pre_get_posts
and create a custom meta_query so I can alter the query within the URL and display babies,children and home books like so:
I’m using the Advanced Custom Fields plugin for my custom fields and my custom field is called ‘type’ and is set up in WordPress as a Checkbox Field, so it accepts multiple values.
For the life of me, I can’t work out how to get it to display the queried posts.
function my_pre_get_posts( $query ) {
if( is_admin() ) { return; }
$meta_query = $query->get('meta_query');
if( !empty($_GET['type']) ) {
$type = explode('|', $_GET['type']);
$meta_query[] = array(
'key' => 'type',
'value' => $type,
'compare' => 'IN',
);
}
$query->set('meta_query', $meta_query); // update the meta query args
return; // always return
}
Using 'compare' => 'LIKE'
just returns random posts, using IN
returns a blank results page.
Not sure if it’s to do with my $_GET['type']
parameter, which is supposed to be string.
Any help with solving this is appreciated. I’m on my 5th cup of coffee and the evening isn’t looking promising.
archive.book.php
$args = array(
'post_type' => 'book',
'posts_per_page' => 10,
);
$wp_query = new WP_Query( $args );
while( $wp_query->have_posts() ) {
$wp_query->the_post();
get_template_part( 'content', get_post_format() );
}
This normal loop:
<?php
print_r( $wp_query->request );
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
get_template_part( 'content', get_post_format() );
} // end while
} // end if
?>
outputs this:
SELECT SQL_CALC_FOUND_ROWS blISt3rs871o_posts.ID FROM blISt3rs871o_posts WHERE 1=1 AND blISt3rs871o_posts.post_type = 'book' AND (blISt3rs871o_posts.post_status = 'publish' OR blISt3rs871o_posts.post_status = 'private') ORDER BY blISt3rs871o_posts.post_date DESC LIMIT 0, 10
First, keep in mind that
pre_get_posts
is a hook that is run on every single query that WP runs, so be very, very careful when utilizing it. That said, it is absolutely the best tool for what you want to do, and certainly better than writing a separate custom query in the page template.I’ve added some additional conditions to the start of this function to make sure it is abandoned early if the query is: not for the main loop, running on an admin page, or if the ‘type’ query string is not present. You should add a few more conditions here to restrict it even further, e.g.
if ( ! is_page( 'something' ) ) { return; }
orif ( ! is_front_page() ) { return; }
.That said, here is some sample code:
This expects the query string to be comma-separated, as you presented it in your example, website.com/whatever/?type=one,two,three. In your code example you were looking for a pipe character, which is what @Foxsk8 was getting at in his comments.
Here is code, that works.