• Hello,
    I have recently add post_tag taxonomy to a custom post type I have on my WP installation (Portfolio) by register_taxonomy_for_object_type() function.
    I have then created a function to output the query result into an archive page the list of post and custom type post (Portfolio) by tag. The following is the snippet code:

    add_filter( 'pre_get_posts', 'my_callback' );
    function my_callback( $query ) {
    if( is_archive() && $query->is_main_query() &&
    	empty( $query->query_vars['suppress_filters'] )) {
    	$query->set( 'post_type', array( 
    			'post',
    			'portfolio') );
    	}
    }

    It works, but Portfolio menu, which should have to show only that kinda custom type posts, now shows standard blog post as well.
    Something wrong with this customization?
    Thanks for your help.

    Riccardo

Viewing 4 replies - 1 through 4 (of 4 total)
  • What’s responsible for the “Portfolio menu”? A plugin, your theme, or your own code?

    Would need to see that to know for sure. Try contacting the developer of whatever’s generating the menu (unless it is you).

    Thread Starter r99photography

    (@r99photography)

    Hello Jacob,
    thanks for your reply. The Portfolio menu comes through a wordpress theme I bought.
    Anyway, it does not matter this way because I have just tried on a fresh WP installation and got the same issue.
    Just for your information, I installed a fresh copy of WP v4.7, I then added this snippet code to the functions.php file:

    // Register custom post type "Gallery"
    add_action('init', 'custom_post_type_callback');
    
    function custom_post_type_callback(){
    	$args = array (
    		'public'	=> true,
    		'label'		=> 'Gallery',
    		'has_archive' => true,
    		'rewrite'	=> array('slug' => 'gallery'),
    		'supports'	=> array(
    			'title', 'editor', 'author', 'thumbnail', 'comments', 'revision'),
    		);
    	register_post_type('gallery', $args);
    }
    
    // Add an existing taxonomies to custom post type "Gallery"
    add_action('init', 'add_taxonomies_gallery_callback');
    
    function add_taxonomies_gallery_callback(){
    	register_taxonomy_for_object_type('post_tag', 'gallery');
    	register_taxonomy_for_object_type('category', 'gallery');
    }
    
    // Output Gallery posts in the archive page
    add_filter ('pre_get_posts', 'add_gallery_posts_query_callback');
    
    function add_gallery_posts_query_callback($query){
    	if ( is_archive() && $query->is_main_query() ) {
    		$query->set('post_type', array (
    			'post',
    			'gallery')
    		);
    	}
    }

    Maybe something wrong with the above snippet? I am not an expert, I must say, just followed one of many articles there are on the net about this task.

    This is the screenshot of Posts menu page:
    https://drive.google.com/open?id=0B-DXE7ThxZ20MWFaSFlBU3lwMXM

    This is the screenshot of Gallery menu page:
    https://drive.google.com/open?id=0B-DXE7ThxZ20UGdibW5HQkZDaEE

    As you can see, in the latter there are 4 posts: two in red are the standard blog posts (as seen in the previous image), two in blue are the custom type posts called “Gallery”. Note also that under the page title is reported the current view should have only 2 published posts, but the query output 4 items.

    I guess something is wrong in the function add_gallery_posts_query_callback I wrote above.

    Thanks if you may help me.
    Bye.

    Riccardo

    Hey Riccardo,

    just replace this function

    
    function add_gallery_posts_query_callback($query){
    	if ( is_archive() && $query->is_main_query() && !is_admin() ) {
    		$query->set('post_type', array (
    			'post',
    			'gallery')
    		);
    	}
    }
    

    i think that will solves your problem ??

    Oh, right, by “Portfolio menu” you mean in the admin. Swayem is correct, you need to specify ! is_admin() in the pre_get_posts action.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom type post menu shows standard blog post’ is closed to new replies.