Wpbakery pagebuilder: How to create dropdown with custom taxonomy terms?

I have custom post type and custom taxonomy with it.
Trying to create wpbakery pagebuilder’s element with a dropdown of terms.
Here is my vc_map function:

vc_map( 
    array(
        'name'                      => __('All category posts', 'text-domain'),
        'base'                      => 'company_category_products',
        'category'                  => __('Company', 'text-domain'),
        'description'               => __('A list of post for choosen category', 'text-domain'),
        "show_settings_on_create"   => true,
        "is_container"              => false,
        "icon"                      => 'custom_icon',
        "params"                    => array(
            array(
                "type"          => "dropdown",
                "heading"       => __("Post category", 'text-domain'),
                'value'         => company_get_terms_array(),
                "param_name"    => "post_category",
                "description"   => __("Choose category with posts", 'text-domain')
            ),
        )
    
    ) 
); 

company_get_terms_array() is a function in my functions.php file, which returns an assoc array of terms’ ids as key and terms’ as values.

function company_get_terms_array($taxonomy = 'product-category'){

    $args = array(
        'taxonomy'  => $taxonomy,
        'parent'    => 0
    ); 

    $terms = get_terms( $args ); 
    $result = array(); 

    if( !empty($terms) && !is_wp_error($terms) ):

        foreach( $terms as $term) { 
           $result( $term->term_id ) = $term->name . ' (' . $term->count . ')'; 
        }

    endif;

    return !empty($result) ? $result : array(); 

}

The function by it self works fine, I can print it in custom post edit page’s metabox for example and get a result as I expect. Like this

Array
(
    (30) => Temp1 (3)
    (31) => Temp2 (2)
    (27) => Temp3 (3)
    (29) => Temp4 (2)
    (28) => Temp5 (4)
    (5)  => Temp6 (2)
    (32) => Temp7 (4)
)

But on settings window of wpbakery pagebuilder’s element I see an empty dropdown, which I return in a function if get_terms() returns wp_error object. Custom post type and custom taxonomy registered with ‘init’ hook and maybe wpbakery pagebuilder call this function earlier?
I have also the second wpbakery element with a dropdown, where I show custom posts. I use almost the same function, but with get_posts instead of get_terms. It works fine.

function company_get_posts_array($post_type = 'custom-posts'){

    $args = array( 'posts_per_page' => -1, 'post_type' => $post_type ); 
    $posts = get_posts( $args ); 
    $result = array(); 

    if( $posts ):

        foreach( $posts as $post) { 
           $result( $post->ID ) = get_the_title( $post->ID ); 
        }

    endif;

    return !empty($result) ? $result : array(); 

} 

custom post types – Archive page for taxonomy terms

How can I create a archive over terms from a custom taxonomy like there is for custom post types? domain.com/taxonomy is empty/404, while domain.com/cpt shows all posts for that CPT.

I know I could loop true all terms and display them on a “static” page that I make myself and apply with the template page-taxonomy.php. This does however not allow me to have a “Next page” function as far as I know.

$terms = get_terms( 'taxonomy' );
foreach ( $terms as $term ) {
    echo $term->name
}

Is there a way to make a “normal” archive for the terms of a taxonomy? I do not want the posts of the terms/taxonomy, but the terms themself.

Taxonomy page not working – WordPress Development Stack Exchange

I have a custom post type called topics, with two custom taxonomies: topicscat and tagscat.
The taxonomy page of tagscat is returning 404 error. I guess Im not registering it right.
Here is my code.

<?php
/**
 * topics ====================================================
 */
add_action('init', 'my_custom_topics');
function my_custom_topics()
{
  $labels = array(
    'name' => _x('Topics', 'post type general name'),
    'singular_name' => _x('topics', 'post type singular name'),
    'add_new' => _x('新しくTopicsを書く', 'topics'),
    'add_new_item' => __('Topics記事を書く'),
    'edit_item' => __('Topics記事を編集'),
    'new_item' => __('新しいTopics記事'),
    'view_item' => __('Topics記事を見る'),
    'search_staff' => __('Topics記事を探す'),
    'not_found' =>  __('Topics記事はありません'),
    'not_found_in_trash' => __('ゴミ箱にTopics記事はありません'),
    'parent_item_colon' => ''
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array(
      "slug" => "topics",
      "with_front" => false
    ),
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => 5,
    'supports' => array('title','editor','excerpt','thumbnail','revisions'),
    'has_archive' => true
  );
  register_post_type('topics',$args);
}



//tags taxonomy

add_action ('init','create_tagscat_taxonomy','0');
function create_tagscat_taxonomy () {
    $taxonomylabels = array(
    'name' => _x('tagカテゴリー','post type general name'),
    'singular_name' => _x('tagカテゴリー','post type singular name'),
    'search_items' => __('tagカテゴリー'),
    'all_items' => __('tagカテゴリー'),
    'parent_item' => __( 'Parent Cat' ),
    'parent_item_colon' => __( 'Parent Cat:' ),
    'edit_item' => __('tagカテゴリーを編集'),
    'add_new_item' => __('tagカテゴリーを書く'),
    'menu_name' => __( 'tagカテゴリー' ),
    );
    $args = array(
    'labels' => $taxonomylabels,
    'hierarchical' => true,
    'has_archive' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'tagscat' )
    );
    register_taxonomy('tagscat','tags',$args);
}
// Rewrite Custom Taxonomy to share base slug with Custom Post Type
function rewrite_tags_category() {
$tags_category_args = get_taxonomy( 'tagscat' ); // returns an object

$tags_category_args->show_admin_tags = true;
$tags_category_args->rewrite('slug') = 'tags';
$tags_category_args->rewrite('with_front') = false;

register_taxonomy( 'tagscat', 'topics', (array) $tags_category_args );
}
add_action( 'init', 'rewrite_tags_category', 11 );


/** ---- Topicsタクソノミー追加 ---- **/
add_action ('init','create_topicscat_taxonomy','0');
function create_topicscat_taxonomy () {
    $taxonomylabels = array(
    'name' => _x('topicsカテゴリー','post type general name'),
    'singular_name' => _x('topicsカテゴリー','post type singular name'),
    'search_items' => __('topicsカテゴリー'),
    'all_items' => __('topicsカテゴリー'),
    'parent_item' => __( 'Parent Cat' ),
    'parent_item_colon' => __( 'Parent Cat:' ),
    'edit_item' => __('topicsカテゴリーを編集'),
    'add_new_item' => __('topicsカテゴリーを書く'),
    'menu_name' => __( 'topicsカテゴリー' ),
    );
    $args = array(
    'labels' => $taxonomylabels,
    'hierarchical' => true,
    'has_archive' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'topicscat' )
    );
    register_taxonomy('topicscat','topics',$args);
}
// Rewrite Custom Taxonomy to share base slug with Custom Post Type
function rewrite_topics_category() {
$topics_category_args = get_taxonomy( 'topicscat' ); // returns an object

$topics_category_args->show_admin_topics = true;
$topics_category_args->rewrite('slug') = 'topics';
$topics_category_args->rewrite('with_front') = false;

register_taxonomy( 'topicscat', 'topics', (array) $topics_category_args );
}
add_action( 'init', 'rewrite_topics_category', 11 );



// Get the Post ID from permalink structure base/CT/Post_ID
function topics_rewrites_init($post_link, $post = 0){
    add_rewrite_rule('topics/((0-9)+)?(page/)?((0-9)+)?/?$', 'index.php?paged=$matches(3)&post_type=topics&p=$matches(1)', 'top');
    add_rewrite_rule('topics/((A-Za-z0-9-_)+)/?(page/)?((0-9)+)?/?$', 'index.php?paged=$matches(3)&topicscat=$matches(1)', 'top');
}
add_action('init', 'topics_rewrites_init');

function cms_links($post_link, $post) {
    if($post->post_type === 'topics') {
        return home_url('topics/' . $post->ID . '/');
    } else {
        return $post_link;
    }
}
add_filter('post_type_link', 'cms_links', 1, 3); 

custom post types – Pagination on taxonomy template not working after rewriting slugs

My pagination is not working on my taxonomy(sub-category) page. It keeps on loading my archive template. This happened after I applied function codes that re-write’s url and producing all parent and child category ony my permalink.

Heres my code under products-post-type.php

<?php

/*
** ----------
** Register Products Post Type
** ----------
*/
function products_post_type() {

    $labels = array(
        'name'                          => _x( 'Brands', 'Post Type General Name', 'text_domain' ),
        'singular_name'                 => _x( 'Product', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'                     => __( 'Products', 'text_domain' ),
        'name_admin_bar'                => __( 'Products', 'text_domain' ),
        'archives'                      => __( 'Products', 'text_domain' ),
        'attributes'                    => __( 'Product Attributes', 'text_domain' ),
        'parent_item_colon'             => __( 'Parent Product:', 'text_domain' ),
        'all_items'                     => __( 'All Products', 'text_domain' ),
        'add_new_item'                  => __( 'Add New Product', 'text_domain' ),
        'add_new'                       => __( 'Add New Product', 'text_domain' ),
        'new_item'                      => __( 'New Product', 'text_domain' ),
        'edit_item'                     => __( 'Edit Product', 'text_domain' ),
        'update_item'                   => __( 'Update Product', 'text_domain' ),
        'view_item'                     => __( 'View Product', 'text_domain' ),
        'view_items'                    => __( 'View Products', 'text_domain' ),
        'search_items'                  => __( 'Search Product', 'text_domain' ),
        'not_found'                     => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'            => __( 'Not found in Trash', 'text_domain' ),
        'featured_image'                => __( 'Featured Image', 'text_domain' ),
        'set_featured_image'            => __( 'Set featured image', 'text_domain' ),
        'remove_featured_image'         => __( 'Remove featured image', 'text_domain' ),
        'use_featured_image'            => __( 'Use as featured image', 'text_domain' ),
        'insert_into_item'              => __( 'Insert into product', 'text_domain' ),
        'uploaded_to_this_item'         => __( 'Uploaded to this product', 'text_domain' ),
        'items_list'                    => __( 'Products list', 'text_domain' ),
        'items_list_navigation'         => __( 'Products list navigation', 'text_domain' ),
        'filter_items_list'             => __( 'Filter products list', 'text_domain' ),
    );

    $args = array(
        'label'                         => __( 'Product', 'text_domain' ),
        'labels'                        => $labels,
        'supports'                      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments'),
        'taxonomies'                    => array( 'product-categories' ),
        'public'                        => true,
        'show_ui'                       => true,
        'show_in_menu'                  => true,
        'menu_position'                 => 5,
        'menu_icon'                     => 'dashicons-screenoptions',
        'show_in_admin_bar'             => true,
        'show_in_nav_menus'             => true,
        'can_export'                    => true,
        'exclude_from_search'           => false,
        'publicly_queryable'            => true,
        'hierarchical'                          => false,
        'has_archive'                           => 'products',
        'show_in_rest'                          => true,
        'capability_type'               => 'post', //...or page
        'rewrite'                                   => array(
                                                                         'slug'                     => 'brands-item',
                                                                         'with_front'       => false,
                                                                         'hierarchical'   => true,
                                                                     ),
    );
    register_post_type( 'products', $args );

}
add_action( 'init', 'products_post_type', 0 );




/*
** ----------
** Register Products Taxonomy - Hierarchical
** ----------
*/

function products_taxonomy_hierarchical() {

    $labels = array(
        'name'                          => _x( 'Brand Categories', 'Taxonomy General Name', 'text_domain' ),
        'singular_name'                 => _x( 'Brand Category', 'Taxonomy Singular Name', 'text_domain' ),
        'menu_name'                     => __( 'Brand Categories', 'text_domain' ),
        'all_items'                     => __( 'All Brand Categories', 'text_domain' ),
        'parent_item'                   => __( 'Parent Brand Category', 'text_domain' ),
        'parent_item_colon'             => __( 'Parent brand category:', 'text_domain' ),
        'new_item_name'                 => __( 'New Brand Category', 'text_domain' ),
        'add_new_item'                  => __( 'Add New Brand Category', 'text_domain' ),
        'edit_item'                     => __( 'Edit Brand Category', 'text_domain' ),
        'update_item'                   => __( 'Update Brand Category', 'text_domain' ),
        'view_item'                     => __( 'View Brand Category', 'text_domain' ),
        'separate_items_with_commas'    => __( 'Separate brand categories with commas', 'text_domain' ),
        'add_or_remove_items'           => __( 'Add or remove brand categories', 'text_domain' ),
        'choose_from_most_used'         => __( 'Choose from the most used', 'text_domain' ),
        'popular_items'                 => __( 'Popular Brand Categories', 'text_domain' ),
        'search_items'                  => __( 'Search Brand Categories', 'text_domain' ),
        'not_found'                     => __( 'Not Found', 'text_domain' ),
        'no_terms'                      => __( 'No items', 'text_domain' ),
        'items_list'                    => __( 'Brand Categories list', 'text_domain' ),
        'items_list_navigation'         => __( 'Brand Categories list navigation', 'text_domain' ),
    );
    $args = array(
        'labels'                        => $labels,
        'hierarchical'                  => true,
        'public'                        => true,
        'show_ui'                       => true,
        'show_admin_column'                     => true,
        'show_in_nav_menus'                     => true,
        'show_in_rest'                              => true,
        'rewrite'                                       => array(
                                                                             'slug'                     => 'brands-categories',
                                                                             'with_front'       => false,
                                                                             'hierarchical'   => true,
                                                                         ),
        // 'rewrite'                        => $rewrite,
    );
    register_taxonomy( 'product-categories', array( 'products' ), $args );

}
add_action( 'init', 'products_taxonomy_hierarchical', 0 );










add_action( 'generate_rewrite_rules', 'register_products_rewrite_rules' );
function register_products_rewrite_rules( $wp_rewrite ) {
    $new_rules = array(
        'brands/((^/)+)/?$' => 'index.php?product-categories=' . $wp_rewrite->preg_index( 1 ),
        'brands/((^/)+)/((^/)+)/?$' => 'index.php?post_type=products&product-categories=' . $wp_rewrite->preg_index( 1 ) . '&products=' . $wp_rewrite->preg_index( 2 ),
        'brands/((^/)+)/((^/)+)/page/(d{1,})/?$' => 'index.php?post_type=products&product-categories=' . $wp_rewrite->preg_index( 1 ) . '&paged=' . $wp_rewrite->preg_index( 3 ),
        'brands/((^/)+)/((^/)+)/((^/)+)/?$' => 'index.php?post_type=products&product-categories=' . $wp_rewrite->preg_index( 5 ) . '&products=' . $wp_rewrite->preg_index( 3 ),
    );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    return $wp_rewrite->rules;
}



// A hacky way of adding support for flexible custom permalinks
// There is one case in which the rewrite rules from register_kb_rewrite_rules() fail:
// When you visit the archive page for a child section(for example: http://example.com/products/category/child-category)
// The deal is that in this situation, the URL is parsed as a Knowledgebase post with slug "child-category" from the "category" section
function fix_products_subcategory_query($query) {
    if ( isset( $query('post_type') ) && 'products' == $query('post_type') ) {
        if ( isset( $query('products') ) && $query('products') && isset( $query('product-categories') ) && $query('product-categories') ) {
            $query_old = $query;
            // Check if this is a paginated result(like search results)
            if ( 'page' == $query('product-categories') ) {
                $query('paged') = $query('name');
                unset( $query('product-categories'), $query('name'), $query('products') );
            }
            // Make it easier on the DB
            $query('fields') = 'ids';
            $query('posts_per_page') = 1;

            // See if we have results or not
            $_query = new WP_Query( $query );
            if ( ! $_query->posts ) {
                $query = array( 'product-categories' => $query('products') );
                if ( isset( $query_old('product-categories') ) && 'page' == $query_old('product-categories') ) {
                    $query('paged') = $query_old('name');
                }
            }
        }
    }

    return $query;
}
add_filter( 'request', 'fix_products_subcategory_query', 10 );


function products_article_permalink( $article_id, $section_id = false, $leavename = false, $only_permalink = false ) {
    $taxonomy = 'product-categories';
    $article = get_post( $article_id );

    $return = '<a href="';
    $permalink = ( $section_id ) ? trailingslashit( get_term_link( intval( $section_id ), 'product-categories' ) ) : home_url( '/brands/' );
    $permalink .= trailingslashit( ( $leavename ? "%$article->post_type%" : $article->post_name ) );
    $return .= $permalink . '/" >' . get_the_title( $article->ID ) . '</a>';
    return ( $only_permalink ) ? $permalink : $return;
}

function filter_products_post_link( $permalink, $post, $leavename ) {
    if ( get_post_type( $post->ID ) == 'products' ) {
        $terms = wp_get_post_terms( $post->ID, 'product-categories' );
        $term = ( $terms ) ? $terms(0)->term_id : false;
        $permalink = products_article_permalink( $post->ID, $term, $leavename, true );
    }
    return $permalink;
}
add_filter( 'post_type_link', 'filter_products_post_link', 100, 3 );

function filter_products_section_terms_link( $termlink, $term, $taxonomy = false ) {
    if ( $taxonomy == 'product-categories' ) {
        $section_ancestors = get_ancestors( $term->term_id, $taxonomy );
        krsort( $section_ancestors );
        $termlink =  home_url( '/brands/' );
        foreach ( $section_ancestors as $ancestor ) {
            $section_ancestor = get_term( $ancestor, $taxonomy );
            $termlink .= $section_ancestor->slug . '/';
        }
        $termlink .= trailingslashit( $term->slug );
    }

    return $termlink;
}
add_filter( 'term_link', 'filter_products_section_terms_link', 100, 3 );


?>

Thank you guys for helping me.

php – Retrieve some post which match a specific taxonomy and print their url and some relative taxonomy/field in a file?

as specified in the title I should create a (php?) file to upload to the server that retrieves the url of the posts that correspond to a certain taxonomy and simply print the url of these (and some other datas) .

Can you give me a tip or suggestion?

Thank you all

custom post types – Get all term IDs from by other taxonomy term

I have a custom post type “objects” that has two taxonomies: “portfolios” and “cities”. I need a function where I can insert a cities taxonomy term id and get an array of all portfolio term ids.

Because the large amount of posts (20.000+) it would great to have a fast solution maybe via direct SQL request.

I have to make two select fields, one for “portfolios” and a second for “cities”. I would like to give the second select options the ids of the first one so that I can filter the second one by the first via Javascript.

Display 1 custom post type by a custom taxonomy not working

So the idea here is I have multiple products (CPT) and a custom taxonomy named Feature. It’s a radio button field with Yes/No option. I want to display 1 random featured product on front page. But somehow it didn’t work, what did I do wrong?

<?php 
                    $args = array(
                        'post_type' => 'product',
                        'orderby' => 'rand',
                        'posts_per_page' => 1,
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'product_feature', //CPT Name
                                'field' => 'product_feature_field', //Field Name
                                'terms' => array( 'product-feature-yes' ) //Taxonomy Slug
                                ),
                            )
                        );
                    $new_query = new WP_Query( $args ); 
                ?>

custom post types – Display tags cloud of current taxonomy term in archive page

I am so sorry that I have no my own code for you because I am not good at php. I am trying to display tags ( with custom post count ) of current custom taxonomy term. It because I will display tags that have been used in the posts this taxonomy term. if any tag that has not been used in posts of this taxonomy term then don’t display. But my tags is also a custom taxonomy ( I don’t use wordpress default tags). Can you help me please. Thanks for your attention. I am trying to do according this answer. But not succeed yet. Some smililar questions:

Count number of posts which exist in 2 taxonomies?

WordPress – Only show tags under the current Category

Display tags cloud from a specific category ID with a shortcode

$currenttax = get_queried_object();
$mytag = get_terms( array(
    'taxonomy' => 'custom-tag',
    'hide_empty' => false,
    'fields' => 'id=>slug' 
) );

$mytax = get_terms( array(
    'taxonomy' => 'my-custom-taxonomy',
    'hide_empty' => false,
    'fields' => 'id=>slug' 
) );


foreach ($mytaxs as $mytax) {

foreach ($mytags as $mytag) {

$args = array(
    'post_type' => 'my-post-type',
 'post_status'=>'publish',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'my-custom-taxonomy',
            'field'    => 'slug',
            'terms'    => array( $currenttax ),
        ),
        array(
            'taxonomy' => 'mytag',
            'field'    => 'slug',
            'terms'    => array( $mytag ),
        ),
    ),
);
$query = new WP_Query( $args );

echo $mytag .": " . $mytax . ": " . $query->post_count . "<br>";

}
}