WordPress.org

Ready to get started?Download WordPress

Forums

Issue with Custom Post Types (4 posts)

  1. mageguild
    Member
    Posted 1 year ago #

    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

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    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'));
    		}
    
    	}
    }
  3. mageguild
    Member
    Posted 1 year ago #

    Yes this has worked perfectly.

    Thanks.

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    No problem :-)

Topic Closed

This topic has been closed to new replies.

About this Topic