Postype with Taxonomy slug

PHOTO EMBED

Tue Oct 10 2023 22:33:56 GMT+0000 (Coordinated Universal Time)

Saved by @hamzakhan123

// Register the custom post type "buy From US"
function create_buy_from_us_post_type() {
    $labels = array(
        'name' => _x('Buy From US', 'Post Type General Name'),
        'singular_name' => _x('Buy From US', 'Post Type Singular Name'),
        'menu_name' => _x('Buy From US', 'Admin Menu text'),
        'name_admin_bar' => _x('Buy From US', 'Add New on Toolbar'),
        'archives' => __('Buy From US Archives'),
        'attributes' => __('Buy From US Attributes'),
        'parent_item_colon' => __('Parent Buy From US:'),
        'all_items' => __('All Buy From US'),
        'add_new_item' => __('Add New Buy From US'),
        'add_new' => __('Add New'),
        'new_item' => __('New Buy From US'),
        'edit_item' => __('Edit Buy From US'),
        'update_item' => __('Update Buy From US'),
        'view_item' => __('View Buy From US'),
        'view_items' => __('View Buy From US'),
        'search_items' => __('Search Buy From US'),
        'not_found' => __('Not found'),
        'not_found_in_trash' => __('Not found in Trash'),
        'featured_image' => __('Featured Image'),
        'set_featured_image' => __('Set featured image'),
        'remove_featured_image' => __('Remove featured image'),
        'use_featured_image' => __('Use as featured image'),
        'insert_into_item' => __('Insert into Buy From US'),
        'uploaded_to_this_item' => __('Uploaded to this Buy From US'),
        'items_list' => __('Buy From US list'),
        'items_list_navigation' => __('Buy From US list navigation'),
        'filter_items_list' => __('Filter Buy From US list'),
    );

    $args = array(
        'labels' => $labels,
        'label' => __('Buy From US'),
        'description' => __(''),
        'menu_icon' => 'dashicons-admin-home',
        'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'page-attributes', 'post-formats'),
       'taxonomies' => array(), // Add the custom taxonomy to the post type
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => true,
        'can_export' => true,
        'has_archive' => true,
        'hierarchical' => true,
        'exclude_from_search' => true,
        'show_in_rest' => true,
        'publicly_queryable' => true,
        'capability_type' => 'post',
	     'rewrite' => array('slug' => 'buy-from-us/%buy_cat%'),
    );

    register_post_type('buy-from-us', $args);

    $taxonomy_args = array(
        'labels' => array(
            'name' => 'Category',
            'singular_name' => 'Category',
            'menu_name' => 'Category',
        ),
        'hierarchical' => true,
        'rewrite' => array('slug' => 'buy_cat', 'with_front' => false),
    );

    register_taxonomy('buy_cat', array('buy-from-us'), $taxonomy_args);
}

add_action('init', 'create_buy_from_us_post_type');


function buy_from_us_loop($atts) {
    ob_start();

    $categories = get_terms(
        array(
            'taxonomy' => 'buy_cat',
            'order' => 'asc'
        )
    );
?>
<div class="row buyus">
    <?php
    foreach ($categories as $category) :
    ?>
        <div class="col-md-3">
            <?php
            $arg = array(
                'post_type' => 'buy-from-us',
                'posts_per_page' => -1,
                'tax_query' => array(
                    array(
                        'taxonomy' => 'buy_cat',
                        'field' => 'slug',
                        'terms' => $category->slug,
                    ),
                ),
            );

            $data = new WP_Query($arg);
            if ($data->have_posts()) {
                echo '<ul>';
                while ($data->have_posts()) {
                    $data->the_post();

                    $category = get_the_category();
                    $buy_cats = get_the_terms(get_the_ID(), 'buy_cat');

                    $category_url = get_category_link($category[0]->term_id);
                    $buy_cat_url = get_term_link($buy_cats[0]->term_id, 'buy_cat');

                   echo '<li><a href="' . get_permalink() .'">';
                    the_title();
                    echo '</a></li>';
                }

                echo '</ul>';
            }
            wp_reset_postdata();
            ?>
        </div>
    <?php endforeach; ?>
</div>
<?php
    return ob_get_clean();
}

add_shortcode('buy_from_us', 'buy_from_us_loop');


function change_buy_permalink( $post_link, $post ) {
if ( is_object( $post ) && $post->post_type == 'buy-from-us' ) {
$terms = wp_get_object_terms( $post->ID, 'buy_cat' );
if ( $terms ) {
return str_replace( '%buy_cat%', $terms[0]->slug, $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'change_buy_permalink', 10, 2 );

content_copyCOPY