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
'compare' => 'IN'still results in a blank page.explode('|'but that still results in a blank results page.print_r( $wp_query->request );to your template, this will show you the SQL query being sent to the database. Copy whatever that outputs, edit your question, and paste it here so we can see what it looks like.