0

I have a problem with display posts in custom taxonomy. I created custom post type m_product and custom taxonomy for this post type m_product_category. I created file in main theme folder named taxonomy-m_product_category.php and maked html for it:

protectFile();

get_header();
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    print_r($wp_query);
    ?>
    <div class="top-bg" style="background-image: url(<?php echo THEME_IMAGES_URI; ?>/ronney-produkty-akcesoria-salon-fryzjerski-str-kategorie-produktow-tlo-01.jpg)"></div>
    <?php
    echo do_shortcode('[meetProducts]');
        if (have_posts()) {
            ?>
            <div class="container py-5">
                <div class="row" id="products-list">
                    <?php
                        while (have_posts()) {
                            the_post();
                            $subtitle = get_field('m_product_subtitle');
                            ?>
                            <div class="col-12 col-md-3 mb-3">
                                <a href="<?php echo the_permalink(); ?>" class="product-box d-block" rel="noopener">
                                    <?php
                                        if (has_post_thumbnail()) {
                                            the_post_thumbnail('thumbnail', ['class' => 'd-block mx-auto img-fluid']);
                                        }
                                    ?>

                                    <div class="text-center mt-3 product-box-title">
                                        <?php
                                            the_title();

                                            if ($subtitle) {
                                                ?>
                                                <div class="product-box-subtitle">
                                                    <?php echo sanitize_text_field($subtitle); ?>
                                                </div>
                                                <?php
                                            }
                                        ?>

                                    </div>
                                </a>
                            </div>
                            <?php
                        }
                    ?>
                </div>

                <?php
                    if ($paged < $wp_query->max_num_pages) {
                        ?>
                        <div class="text-center mt-4">
                            <button type="button" class="load-more-products btn btn-secondary btn-sm" data-next-page="<?php echo esc_attr($paged + 1); ?>" data-max-page="<?php echo esc_attr($wp_query->max_num_pages); ?>">Wczytaj więcej</button>
                        </div>
                        <?php
                    }
                ?>
            </div>
            <?php
            wp_reset_postdata();
        } else {
            echo 'test';
        }
    echo do_shortcode('[meetProducts]');
get_footer();

But when i go to the custom taxonomy page, can't display posts in while loop, i mean is nothing to see. I didn't make any functions which should change some queries for taxonomies. Archive working properly, i don't have set public to false or something for register taxonomy, I will show you register taxonomy code:

// Protect file
protectFile();

function custom_taxonomy_m_product_category() {
    register_taxonomy('m_product_category', ['m_product'], [
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'exclude_from_search' => false,
        'query_var' => true,
        'public' => true,
        'rewrite' => [
            'slug' => 'cat-slug'
        ],
    ]);
}

add_action('init', 'custom_taxonomy_m_product_category', 0);

I rewrited link in settings but this doesn't help. I don't have idea why this isn't working, could someone give me advice? Thanks!

//Edit: Here is a code for register custom_post_type:

function custom_post_type_m_product() {    
    $args = [
        'label'               => __('Produkty'),
        'description'         => __('Produkty'),
        'labels'              => $labels,
        'supports'            => [
            'title',
            'revisions',
            'thumbnail',
            'excerpt'
        ],
        'taxonomies' => [
            'm_product_category',
            'm_product_color'
        ],
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 6,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => true,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
        'rewrite'             => [
            'slug' => 'produkty'
        ],
        'menu_icon'           => 'dashicons-cart'
    ];

    register_post_type('m_product', $args);

}

add_action('init', 'custom_post_type_m_product', 0);

//Edit2:

I found solution with community help, iI had set exclude_from_search for custom post type to true. Thanks for your patience, cheers!

3
  • Have you tried 'has_archive' => true ? Oct 10, 2018 at 10:49
  • @PratikPatel Hi, yes, you can check post, i edited it, i have set has_archive to true.
    – Adrian
    Oct 10, 2018 at 10:52
  • 1
    @PratikPatel Love you dude, i found a problem, "exclude_from_search" is set to true...
    – Adrian
    Oct 10, 2018 at 10:53

1 Answer 1

0

try this code :

function custom_taxonomy_m_product_category() {
        register_taxonomy('m_product_category', ['m_product'], [
            'hierarchical' => true,
            'labels' => $labels,
            'show_ui' => true,
            'show_admin_column' => true,
            'exclude_from_search' => **false**,
            'query_var' => true,
            'public' => true,
            'hierarchical' => true,
            'rewrite' => ['slug' => 'cat-slug', 'with_front' => true ],
        ]);
    }
1
  • Solution is in the main thread.
    – Adrian
    May 8, 2019 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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