The ‘pre_get_posts’ action doesn’t work for page requests (page templates).
Create a specialized page template:
http://codex.wordpress.org/Page_Templates#Specialized_Page_Template
And do the query in that template file with new WP_Query:
http://codex.wordpress.org/Function_Reference/WP_Query
Example of query:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => array( 'movies' ),
'paged' => $paged
);
// the query
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- loop stuff here -->
<?php endwhile; ?>
<?php endif ?>
The ‘pre_get_posts’ action doesn’t work for page requests (page templates).
It doesn’t? Sheesh! If I’d have known that, I wouldn’t have spent 2 hours tearing my hair out last week! Fancy updating the Codex page to include this rather important factoid? π
Thread Starter
jana26
(@jana26)
Thanks for the quick answers. So how can I list custom post types and filter them with ajax on a page template?
@esmi
We’ve all spend those 2 hours π
You probably can get it to work by unsetting private properties like ‘is_page’, ‘is_singular’, ‘query’ etc. Not a good idea.
I will change the codex page when I find the time.
Tried all of those conditionals. Still couldn’t get pre_get_posts() to work for a custom page template. I sorted it by reverting to query_posts() – which I was hoping to avoid. A pity but never mind.
@jana26: Sorry for the interruption to your topic. If you review the Post Type parameters for wp_Query(), you’ll see that you can limit (or extend) the new query to include custom post types. As for the ajax, you may need to enqueue your .js based on the is_page_template() conditional.
Thread Starter
jana26
(@jana26)
Ok, thank you guys. @esmi: no problem @keesiemeijer: can you combinate time and cats with that plugin? Couldn’t see it in the demo.
You need to ask the plugin’s developer about that.
@esmi
This seems to work. Use pre_get_posts like this:
add_action( 'pre_get_posts', 'my_post_queries' );
function my_post_queries( $query ) {
// change (main) query only on the front end of the website
if ( !is_admin() && $query->is_main_query() ) {
if ( is_page() ) {
// example query
$query->set( 'cat', 3 );
// remove page properties (after query)
$query = remove_page_properties( $query );
}
}
}
And add these two functions to your theme’s functions.php:
function remove_page_properties( $query ) {
if( !is_page() || is_admin() || !$query->is_main_query() )
return $query;
// unset 'page' properties
$query->set( 'pagename', '' );
global $wp_rewrite;
if ( !$wp_rewrite->using_permalinks() ) {
// these properties are set if the default permalink structure is used
$query->set( 'page_id', 0 );
$query->set( 'p', 0 );
}
$query->is_page = '';
$query->is_singular = '';
// add 'page' properties back (after posts are fetched from the database)
// so it will use the correct page template file
add_filter( 'the_posts', 'add_page_properties_back', 10, 2 );
return $query;
}
function add_page_properties_back( $posts, $_this ) {
if ( isset( $_this->query['pagename'] ) )
$_this->set( 'pagename', $_this->query['pagename'] );
global $wp_rewrite;
if ( !$wp_rewrite->using_permalinks() ) {
// these properties are set if the default permalink structure is used
$_this->set( 'page_id', $_this->query['page_id'] );
$_this->set( 'p', $_this->query['page_id'] );
// reset queried_object to correct page
$_this->queried_object = get_post( $_this->query['page_id'] );
$_this->queried_object_id = $_this->queried_object->ID;
}
$_this->is_page = 1;
$_this->is_singular = 1;
return $posts;
}
This to me feels like a hack and is worse than using query_posts (or new WP_Query) on a specialized page template. I would not recommend using it.