Resolved – category-slug.php not working after wordpress migration

I have one issue I’m trying to resolve hours ago searching around the web but can’t by now. Any Idea or clue is welcome…

I’m trying to migrate a WordPress site that use a plugin (CCTM (that have no more development activity)) to register custom post type and fields “recetas” that use a native wordpress category “recetas” in their posts.

In the new build, I register the custom post type manually on functions.php and import the content via native XML importer tool of wordpress.

add_action( 'init', 'codex_book_init' );
function codex_book_init() {
    $labels = array(
        'name'               => _x( 'Recetas'),
        'singular_name'      => _x( 'Receta'),
        'menu_name'          => _x( 'Recetas'),
        'name_admin_bar'     => _x( 'Recetas'),
        'add_new'            => _x( 'Agregar Nueva'),
        'add_new_item'       => __( 'Agregar Nueva Receta'),
        'new_item'           => __( 'Nueva Receta'),
        'edit_item'          => __( 'Editar Receta'),
        'view_item'          => __( 'Ver Receta'),
        'all_items'          => __( 'Todas las Recetas'),
        'search_items'       => __( 'Buscar Receta'),
        'parent_item_colon'  => __( 'Receta Padre:'),
        'not_found'          => __( 'Sin Recetas encontradas.'),
        'not_found_in_trash' => __( 'Sin Recetas encontradas en papelera.')
    );
    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Recetas'),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'recetas'),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => true,
        'menu_position'      => 5,
        'menu_icon'          => 'dashicons-admin-post',
        'taxonomies'         => array( 'category' ),
        'supports'           => array( 'title', 'thumbnail', 'excerpt', 'editor', 'comments')
    );
    register_post_type( 'recetas', $args ); 
}

All content gets ok on single articles from the custom post type, and in new loops WP_Query( array('posts_type'=> 'recetas') the content gets ok too. but the problems comes in the category template (category-recetas.php) used to fetch the post type articles with te default wordpress loop <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>. It simple not working, no one post comes from the category “recetas”.

I try register custom-taxonomy “recetas”, try category-id.php, try archive-slug.php, try resaving permalinks, but nothing works…

Any idea is very usefull and wellcome.
Thanks!

Resolved Thank to @Max Yudin – his answer fixed the issue

1 Answer
1

Category is the built-in taxonomy for posts only, not custom post types. So you have to call the pre_get_posts hook.

This hook is called after the query variable object is created, but
before the actual query is run.

Place the following code to the functions.php or a custom plugin. Not tested though.

<?php
add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
    if( is_category() ) {
        $post_type = get_query_var('post_type');
        if(!$post_type) {
            $post_type = array('nav_menu_item', 'post', 'recetas'); // don't forget nav_menu_item to allow menus to work!
        }
        $query->set('post_type', $post_type);
        return $query;
        }
}

This is slightly modified code from here (wpbeginner.com).

If this does not work, use the original code from the link above.

Leave a Comment