Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example for add a Custom Post Type to the WordPress Loop
// add custom post type to wp loop
add_action( 'pre_get_posts', 'add_to_query' );
// ads to query
function add_to_query( $query ) {
if ( is_admin() || is_preview() )
return;
// only filter on front page
if ( is_home() || is_front_page() && ( FALSE == $query -> query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array( 'post', 'my_post_type' ) );
return $query;
}
@gr4y

This comment has been minimized.

Copy link

@gr4y gr4y commented Jun 1, 2011

I just found out that my preview is not working. And here is the fix:

if ( is_admin() || is_preview() ) return; 

instead of

 if ( is_admin() ) return;

;-)

@bueltge

This comment has been minimized.

Copy link
Owner Author

@bueltge bueltge commented Jun 2, 2011

@gr4y: also here a Thanks! - i have fixed the source.

@gr4y

This comment has been minimized.

Copy link

@gr4y gr4y commented Jun 7, 2011

And I've found another issue. Simply add a check if is_page() and then your pages will work again. ;-)

@bueltge

This comment has been minimized.

Copy link
Owner Author

@bueltge bueltge commented Jun 7, 2011

I think it is better you use is_singular() for posts and pages or use the line 11 for only add the filter on a specific sceanrio - id ( ! is_singular() && ( FALSE == $query -> query_vars['suppress_filters'] ) ) .

@bueltge

This comment has been minimized.

Copy link
Owner Author

@bueltge bueltge commented Jun 9, 2011

Hook int correcty is an action --> add_action(); not add_filter() - Sorry; its fixed on example

@gr4y

This comment has been minimized.

Copy link

@gr4y gr4y commented Jun 9, 2011

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment