Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

jepper on "Removed category from permalink, now custom posts displays as archive"

$
0
0

Hi,

In an attempt to remove slugs for a custom post type I've followed this guide:

http://ryansechrest.com/2013/04/remove-post-type-slug-in-custom-post-type-url-and-move-subpages-to-website-root-in-wordpress

With some assistance of a helpful forum member here I've got it to work for my website. However the changed permalink (without category) and the old link now lead to an archive page. (is_archive = true)
I've set all the flags correctly, for example has_archive = false.

With some is_type hackery (as in fast and ugly) in single.php I've got a normalish website flow back but i'm running into trouble with custom comment fields.

This is the declaration code:

function register_cpt_product() {

    $labels = array(
        'name' => _x( 'Producten', 'product' ),
        'singular_name' => _x( 'Product', 'product' ),
        'add_new' => _x( 'Add New', 'product' ),
        'add_new_item' => _x( 'Add New Product', 'product' ),
        'edit_item' => _x( 'Edit Product', 'product' ),
        'new_item' => _x( 'New Product', 'product' ),
        'view_item' => _x( 'View Product', 'product' ),
        'search_items' => _x( 'Search Products', 'product' ),
        'not_found' => _x( 'No producten found', 'product' ),
        'not_found_in_trash' => _x( 'No producten found in Trash', 'product' ),
        'parent_item_colon' => _x( 'Parent Product:', 'product' ),
        'menu_name' => _x( 'Producten', 'product' ),
    );

    $args = array(
        'labels' => $labels,
        'hierarchical' => false,

        'supports' => array( 'title', 'editor', 'comments' ),
        'taxonomies' => array( 'category' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 20,

        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => false,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => array('slug' => '/','with_front' => true)
        //'capability_type' => 'post'
    );

    register_post_type( 'product', $args );
}

and this the flow specific code:

function custom_post_type_link($permalink, $post, $leavename) {
    global $post;

    //var_dump($post);

    if (!gettype($post) == 'post') {
        return $permalink;
    }
    switch ($post->post_type) {
        case 'product':
           $permalink = get_home_url() . '/' . $post->post_name . '.html';
           //$permalink = "je-moeder.html";
           //var_dump($permalink);
           return $permalink;
           //break;
    }

    return $permalink;
}
add_filter('post_type_link', 'custom_post_type_link',10,3);

function custom_pre_get_posts($query) {
    global $wpdb;

    if (is_main_query() ) {
      $name = $query->get('category_name');
      $name = str_replace(".html", "", $name);

      $post_type = $wpdb->get_var( $wpdb->prepare( 'SELECT post_type FROM ' . $wpdb->posts . ' WHERE post_name = %s LIMIT 1', $name ) ); 

      if($post_type == "product"){
          //$query->set('name', $name );
          $query->set('post_type', "product");
          $query->set("product", $name ); //parser ignores query vars it does not recognize
          $query->set('category_name', "");
          $query->is_single = true;
        }
   }

    return $query;
}
add_action('pre_get_posts','custom_pre_get_posts');

I can't find similar help topics anywhere.

The custom post type is nothing special, no strange plugins running.

Anybody got an idea whats going on, and possibly how to fix this?

Cheers!

Jasper


Viewing all articles
Browse latest Browse all 8245

Trending Articles