WordPress.org

Support

Support » How-To and Troubleshooting » Issue with Custom Post Types

Issue with Custom Post Types

  • mageguild

    @mageguild

    Background

    I have enabled multiple post types on my site and I am using the same tags across all post types. The first issue that I had was the custom posts where not appearing in the tag archive. I found a solution to the issue on this page which suggested adding the following code into my functions.php file which resolved the issue.

    add_filter('pre_get_posts', 'query_post_type');
    function query_post_type($query) {
      if(is_tag()) {
        $post_type = get_query_var('post_type');
    	if($post_type)
    	    $post_type = $post_type;
    	else
    	    $post_type = array('post','video'); // replace cpt to your custom post type
        $query->set('post_type',$post_type);
    	return $query;
        }
    }

    New Problem

    Since adding the code, when I save a post (standard post or custom page), I get a PHP error despite the post saving successfully.

    Warning: Cannot modify header information – headers already sent by (output started at /home/tbassett/webapps/thetennisbulletin/wp-content/themes/StudioBlue/functions.php:61) in /home/tbassett/webapps/thetennisbulletin/wp-includes/pluggable.php on line 881

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator keesiemeijer

    @keesiemeijer

    Try it with this:

    add_action('pre_get_posts', 'query_post_type');
    function query_post_type($query) {
    	// do not alter the query on wp-admin pages and only alter it if it's the main query
    	if (!is_admin() && $query->is_main_query()){
    
    		if(is_tag()) {
    			$query->set('post_type', array('post','video'));
    		}
    
    	}
    }

    mageguild

    @mageguild

    Yes this has worked perfectly.

    Thanks.

    Moderator keesiemeijer

    @keesiemeijer

    No problem 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Issue with Custom Post Types’ is closed to new replies.