Function error – Use of undefined constant ‘pre_get_posts’ – assumed ‘‘pre_get_posts’’

On my functions I have this code to display categories archive for custom posts.

function add_custom_types_to_tax( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars[‘suppress_filters’] 
) ) {

// Get all your post types
$post_types = get_post_types();

$query->set( ‘post_type’, $post_types );
return $query;
}
}
add_filter( ‘pre_get_posts’, ‘add_custom_types_to_tax’ );

But I am getting the following error messages:

Use of undefined constant ‘pre_get_posts’ – assumed ‘‘pre_get_posts’’

Use of undefined constant ‘add_custom_types_to_tax’ – assumed ‘‘add_custom_types_to_tax’’

How to I get this to work without the errors?

1 Answer
1

Those look like curly apostrophes, not straight… try changing them to straight (all of them in your code) like this:

function add_custom_types_to_tax( $query ) {
    if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
        // Get all your post types
        $post_types = get_post_types();
        $query->set( 'post_type', $post_types );
        return $query;
    }
}
add_filter( 'pre_get_posts', 'add_custom_types_to_tax' );

Leave a Comment