0

On a website made with a purchased template, a custom post-type had an archive page. I didn't want that archive page, so I altered the custom post-type in a child-theme like so:

function remove_archive(){
    $portfolio = get_post_type_object( 'portfolio' );
    $portfolio->has_archive = FALSE;
    register_post_type( 'portfolio', $portfolio );
}
add_action( 'init', 'remove_archive', 20 );

This removed the custom post-type archive. However, it was my intention that the url /portfolio would be freed up for use for a page. But even after refreshing the permalinks, clearing caches etc. the url doesn't point to the page. Instead it points to the blog page, which hasn't been set in the settings at all.

The solution should be a situation in where /portfolio is a page, and /portfolio/* is the URL structure for the posts. However, at this moment, that is not working in any way.

Am I missing something?

Edit

As per Pieter's comment, I've added the template filter template_include:

function uncode_redirect_page($original_template) {
    global $is_redirect;
    $is_redirect_active = ot_get_option('_uncode_redirect');
    if ($is_redirect_active === 'on') {
        if(! is_user_logged_in() )
    {
        $is_redirect = true;
      return get_template_directory() . '/redirect-page.php';
    }
    }
    return $original_template;
}
add_filter('template_include', 'uncode_redirect_page');
1
  • Any suggestions on how to further improve my question are also very welcome. Feb 24, 2016 at 14:01

2 Answers 2

1

If you are using WordPress 4.4+ (which you should), you can make use of the register_post_type_args filter to adjust the arguments passed to register_post_type() when the post type is registered. In your child theme, you can do the following

add_filter( 'register_post_type_args', function( $args, $post_type )
{
    // Make sure we only target the portfolio post type
    if ( 'portfolio' !== $post_type )
        return $args;

    /**
     * We are currently registering the portfolio post type, lets continue
     * For debugging purposes, you can do the following inside the filter
     * ?><pre><?php var_dump($args); ?></pre><?php
     *
     * Modify the arguments as needed
     */
    $args['has_archive'] = false;
    $args['rewrite']     = true;

    return $args;   
}, PHP_INT_MAX, 2);

Just remember to flush your rewrite rules after adding the filter. Make sure you adjust the filter to your needs. This should take care of the template load, you should see the page loaded which you created in the back end when you visit http://example.com/portfolio/ (if your created page has a slug of portfolio, ie a permalink structure of http://example.com/portfolio/).

If this does not work, you have something inside your theme which is causing this issue, more likely a pre_get_posts action which is suppose to target the portfolio post type page. You should also consider a bad rewrite as well

EDIT

The final issue was that the template_include filter as mentioned in the question was causing the issue. Removing the filter in the child theme and reflushing the permalinks solved the issue

4
  • This is a great piece of code. Thanks. Unfortunately I think you are spot on with the last part of your answer. I think the theme is causing some issues with the rewrites and will look at that. If that is the issue I'll mark your answer as correct. Until then I'll keep hoping someone will be able to point me to another solution. Feb 26, 2016 at 8:35
  • I think what you should do is, deactivate all plugins and clear all caches. See if this solves your issue. If not, the issue has to be in your theme. Exactly what, I don't know unfortunately Feb 26, 2016 at 10:13
  • 1
    Refreshing the permalinks after removing the filter solved this issue. Added to your code is the following (which is very theme specific, I know) remove_filter('template_include', 'uncode_redirect_page');, which removes the filter, solving the problem. Thanks a lot for your time and effort! Feb 26, 2016 at 10:16
  • 1
    My pleasure, glad you solved it. Thought that filter would be the issue. Enjoy ;-) Feb 26, 2016 at 10:18
0

The archive page is created according to the name of slug of post type so if you want to portfolio as simple page not the archive page just change the slug of portfolio post type. It will solve your problem.

$labels = array(
        'name'               => _x( 'Blog', 'post type general name', 'your-plugin-textdomain' ),
        'singular_name'      => _x( 'Blog', 'post type singular name', 'your-plugin-textdomain' ),
        'menu_name'          => _x( 'Blog', 'admin menu', 'your-plugin-textdomain' ),
        'name_admin_bar'     => _x( 'Blog', 'add new on admin bar', 'your-plugin-textdomain' ),
        'add_new'            => _x( 'Add New', 'Blog', 'your-plugin-textdomain' ),
        'add_new_item'       => __( 'Add New Blog', 'your-plugin-textdomain' ),
        'new_item'           => __( 'New Blog', 'your-plugin-textdomain' ),
        'edit_item'          => __( 'Edit Blog', 'your-plugin-textdomain' ),
        'view_item'          => __( 'View Blog', 'your-plugin-textdomain' ),
        'all_items'          => __( 'All Blog', 'your-plugin-textdomain' ),
        'search_items'       => __( 'Search Blog', 'your-plugin-textdomain' ),
        'parent_item_colon'  => __( 'Parent Blog:', 'your-plugin-textdomain' ),
        'not_found'          => __( 'No Blog found.', 'your-plugin-textdomain' ),
        'not_found_in_trash' => __( 'No Blog found in Trash.', 'your-plugin-textdomain' )
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,

        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'test' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,

        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt',)
    );
    register_post_type( 'Blog', $args );
1
  • That is a solution, but not the solution I'm looking for, as the rest of the custom post-type must function under the same URL. So /portfolio must be a page and /portfolio/* must remain the URL-structure for the posts. Feb 25, 2016 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.