Random Products are not showing the amount of post which i include in posts_per_page - wordpress

Random Products are not showing the amount of post which i include in posts_per_page
this is the criteria which i used to call products
$cpsc_args = array(
'post_type' => 'product',
'posts_per_page' => 8,
'order' => 'desc',
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'pwb-brand',
'field' => 'term_id',
'terms' => $cpsc_term_id_display,
'operator' => 'IN'
)
)
);
$cpsc_query = new WP_Query( $cpsc_args );
now the results amount of products are shuffling sometimes it shows 7 products or 8 sometimes or 9 on every refresh.
here is the plugin file code where i loop the plugin.
<?php
/*
Plugin Name: Sultan Custom Brands
Description: This will Display All the Posts or any Post type as you want.
Version: 1.0
Author: Sultan Gillani
Author URI: https://nya.smyckendahls.se/
Text Domain: sultan-cpsc-plugin
*/
//include_once('functions.php');
function form_create($attr){
$cpsc_terms = get_terms( array(
'taxonomy' => 'pwb-brand',
'hide_empty' => false,
) );
$attr = shortcode_atts(array(
"cpsc_posts_per_page" => -1,
"cpsc_order" => "DESC",
"cpsc_orderby" => "date",
"cpsc_terms_show" => $cpsc_terms[0]->slug,
"cpsc_columns" => 4
),$attr);
$cpsc_term_id = get_term_by( 'slug', $attr['cpsc_terms_show'], 'pwb-brand');
$cpsc_term_id_display = $cpsc_term_id->term_id;
$cpsc_args = array(
'post_type' => 'product',
'posts_per_page' => $attr['cpsc_posts_per_page'],
'order' => $attr['cpsc_order'],
'orderby' => $attr['cpsc_orderby'],
'tax_query' => array(
array(
'taxonomy' => 'pwb-brand',
'field' => 'term_id',
'terms' => $cpsc_term_id_display,
'operator' => 'IN'
)
)
);
$cpsc_query = new WP_Query( $cpsc_args );
?>
<div class="woocommerce columns-<?php echo $attr['cpsc_columns']; ?>">
<?php
wc_get_template_part('loop/loop','start');
if ( $cpsc_query->have_posts() ) {
while ( $cpsc_query->have_posts() ) {
$cpsc_query->the_post();
wc_get_template_part('content','product');
}
// Restore original Post Data
wp_reset_postdata();
}
wc_get_template_part('loop/loop','end');
?>
</div>
<?php
}
add_shortcode('cpsc_code','form_create');
function cpsc_vc_element(){
$brand_arr = [];
$cpsc_terms_data = get_terms( array(
'taxonomy' => 'pwb-brand',
'hide_empty' => false,
) );
$brand_arr['Select The Brand'] = '';
foreach($cpsc_terms_data as $cpsc_term_data){
$brand_arr[$cpsc_term_data->name] =$cpsc_term_data->slug;
}
vc_map(
array(
'name' => __('Brand Query Builder'),
'base' => 'cpsc_code',
'category' => __('Test Shortcodes'),
"icon" => "icon-wpb-woocommerce", // Simply pass url to your icon here
"content_element" => true,
'admin_enqueue_css' => array('/wp-content/plugins/brand_plugin/cpsc.css'),
'params' => array(
array(
"type" => "number",
"holder" => "div",
"class" => "cpsc_plugin_posts_per_page",
"heading" => __( "Posts Per Page", "sultan-cpsc-plugin" ),
"param_name" => "cpsc_posts_per_page",
"value" => "0",
"description" => __( "Enter the number of post to display.", "sultan-cpsc-plugin" )
),
array(
"type" => "dropdown",
"holder" => "div",
"class" => "cpsc_plugin_order",
"heading" => __( "Order", "sultan-cpsc-plugin" ),
"param_name" => "cpsc_order",
'value' => array(
__( 'Orber By', "sultan-cpsc-plugin" ) => '',
__( 'Ascending', "sultan-cpsc-plugin" ) => 'ASC',
__( 'Descending', "sultan-cpsc-plugin" ) => 'DESC',
),
"description" => __( "Order Your Post", "sultan-cpsc-plugin" )
),
array(
"type" => "dropdown",
"holder" => "div",
"class" => "cpsc_plugin_orderby",
"heading" => __( "Order By", "sultan-cpsc-plugin" ),
"param_name" => "cpsc_orderby",
'value' => array(
__( 'Select The Orber With', "sultan-cpsc-plugin" ) => '',
__( 'Date', "sultan-cpsc-plugin" ) => 'date',
__( 'ID', "sultan-cpsc-plugin" ) => 'ID',
__( 'About the Author', "sultan-cpsc-plugin" ) => 'author',
__( 'Title', "sultan-cpsc-plugin" ) => 'title',
__( 'Modified', "sultan-cpsc-plugin" ) => 'modified',
__( 'Random', "sultan-cpsc-plugin" ) => 'rand',
__( 'Comment Count', "sultan-cpsc-plugin" ) => 'comment_count',
__( 'Menu Order', "sultan-cpsc-plugin" ) => 'menu_order',
__( 'Menu Order & title', "sultan-cpsc-plugin" ) => 'menu_order title',
),
"description" => __( "Order by the criteria.", "sultan-cpsc-plugin" )
),
array(
"type" => "dropdown",
"holder" => "div",
"class" => "cpsc_plugin_terms_show",
"heading" => __( "Brands", "sultan-cpsc-plugin" ),
"param_name" => "cpsc_terms_show",
'value' => $brand_arr,
"description" => __( "All Brands", "sultan-cpsc-plugin" )
),
array(
"type" => "textfield",
"holder" => "div",
"class" => "cpsc_plugin_columns",
"heading" => __( "Columns", "sultan-cpsc-plugin" ),
"param_name" => "cpsc_columns",
"value" => "4",
"description" => __( "", "sultan-cpsc-plugin" )
),
)
)
);
}
add_action('vc_before_init','cpsc_vc_element');
?>

Var_dump your:
$cpsc_terms[0]->slug
My quess is that the slug varies per page request? If so, the query will be based on a different term_id each request (if random). And term_id 1 might have 7 posts related, where term_id 2 has f.e. 9 posts related, etc. Hence the number of posts shown.

Related

Filtering custom posts on Elementor using the value of a custom field

I am using the Reveal theme to create a listing website and I recently discovered the Advanced Custom Forms (Custom fields).
On my homepage, there is an Elementor block of this Reveal plugin that allows me to display custom posts.
I'd like to adapt an Elementor widget to filter out listings with a value of 1 in the "FreeListing" custom field (basically exclude them).
I saw that this link offered the solution : How to Filter Elementor Posts By a Custom Field's value in wordpress To make a long story short, everyone adds this to add_action, according to my research, but I have never seen this in my home.
But in my code I don't see how to introduce this. Do you know if I'm editing in the wrong place, or if I can do it here?
<?php
namespace Reveal\Core\Elementor\Widgets;
use Elementor\Controls_Manager;
use Elementor\Widget_Base;
class Popular_Listings_TwoNoFreeListing extends Widget_Base {
public function get_name() {
return 'reveal_popular_listings_2';
}
public function get_title() {
return esc_html__( 'Popular Listings No Free', 'reveal-core' );
}
public function get_icon() {
return 'eicon-banner';
}
public function get_categories() {
return array( 'reveal-core' );
}
protected function register_controls() {
$this->start_controls_section(
'general',
array(
'label' => esc_html__( 'General', 'reveal-core' ),
)
);
$this->add_control(
'title',
array(
'label' => esc_html__( 'Title', 'reveal-core' ),
'label_block' => true,
'type' => Controls_Manager::TEXT,
'default' => __( 'Most Popular Grid', 'reveal-core' ),
)
);
$this->add_control(
'showposts',
array(
'label' => esc_html__( 'Number Of Listing', 'reveal-core' ),
'type' => Controls_Manager::TEXT,
'default' => 6,
)
);
$this->add_control(
'orderby',
array(
'label' => esc_html__( 'Order By', 'reveal-core' ),
'type' => Controls_Manager::SELECT,
'options' => array(
'date' => esc_html__( 'Date', 'reveal-core' ),
'id' => esc_html__( 'ID', 'reveal-core' ),
'title' => esc_html__( 'Title', 'reveal-core' ),
'name' => esc_html__( 'Name', 'reveal-core' ),
'modified' => esc_html__( 'Modified', 'reveal-core' ),
'rand' => esc_html__( 'Random', 'reveal-core' ),
),
'default' => 'date',
)
);
$this->add_control(
'order',
array(
'label' => esc_html__( 'Sort Order', 'reveal-core' ),
'type' => Controls_Manager::SELECT,
'options' => array(
'desc' => esc_html__( 'Descending', 'reveal-core' ),
'asc' => esc_html__( 'Ascending', 'reveal-core' ),
),
'default' => 'desc',
)
);
$this->add_control(
'extra_class',
array(
'label' => esc_html__( 'Extra Class', 'reveal-core' ),
'label_block' => true,
'type' => Controls_Manager::TEXT,
'default' => 'popular-listing',
)
);
$this->end_controls_section();
$this->start_controls_section(
'style_section',
array(
'label' => __('Color Option', 'reveal-core'),
'tab' => \Elementor\Controls_Manager::TAB_STYLE,
)
);
$this->start_controls_tabs( 'tabs_button_style' );
$this->start_controls_tab(
'tab_button_normal',
array(
'label' => __( 'Normal', 'elementor' ),
)
);
$this->add_control(
'icon_color',
array(
'label' => __('Icon Color', 'reveal-core'),
'separator' => 'before',
'type' => \Elementor\Controls_Manager::COLOR,
'selectors' => array(
'{{WRAPPER}} .cat-icon i' => 'background: {{VALUE}} !important',
),
)
);
$this->add_control(
'icon_color_two',
array(
'label' => __('Icon Color Two', 'reveal-core'),
'separator' => 'before',
'type' => \Elementor\Controls_Manager::COLOR,
'selectors' => array(
'{{WRAPPER}} .bg-d' => 'background: {{VALUE}} !important',
),
)
);
$this->add_control(
'rating_color',
array(
'label' => __('Rating Color', 'reveal-core'),
'separator' => 'before',
'type' => \Elementor\Controls_Manager::COLOR,
'selectors' => array(
'{{WRAPPER}} .property_item .list-rate' => 'background: {{VALUE}} !important',
),
)
);
$this->add_control(
'checkek',
array(
'label' => __('Checked Icon Color', 'reveal-core'),
'separator' => 'before',
'type' => \Elementor\Controls_Manager::COLOR,
'selectors' => array(
'{{WRAPPER}} span.veryfied-author:before' => 'background: {{VALUE}} !important',
),
)
);
$this->add_control(
'status_color',
array(
'label' => __('Status Color', 'reveal-core'),
'separator' => 'before',
'type' => \Elementor\Controls_Manager::COLOR,
'selectors' => array(
'{{WRAPPER}} .place-status.closed' => 'color: {{VALUE}} !important',
),
)
);
$this->end_controls_tab();
$this->start_controls_tab(
'tab_button_hover',
array(
'label' => __( 'Hover', 'elementor' ),
)
);
$this->add_control(
'title_color',
array(
'label' => __('Title Hover Color', 'reveal-core'),
'separator' => 'before',
'type' => \Elementor\Controls_Manager::COLOR,
'selectors' => array(
'{{WRAPPER}} .proerty_text .captlize a:hover' => 'color: {{VALUE}} !important',
'{{WRAPPER}} .listing-cat a:hover' => 'color: {{VALUE}} !important',
),
)
);
$this->end_controls_tab();
$this->end_controls_tabs();
$this->end_controls_section();
}
protected function render() {
$settings = $this->get_settings_for_display();
$extra_class = $settings['extra_class'];
$title = $settings['title'];
$showposts = $settings['showposts'];
$orderby = $settings['orderby'];
$order = $settings['order'];
$args = array(
'post_type' => 'rlisting',
'post_status' => 'publish',
'showposts' => $showposts,
'orderby' => $orderby,
'order' => $order,
);
$listing_query = new \WP_Query( $args );
?>
<!-- ================ List Grid Style ======================= -->
<section class="<?php echo $extra_class; ?>">
<div class="container">
<div class="row">
<!-- Single List -->
<?php
while ($listing_query->have_posts()) {
$listing_query->the_post();
?>
<div class="col-lg-4 col-md-6 col-sm-12">
<?php
echo do_shortcode('[classical-list-item]');
?>
</div>
<?php
}
?>
</div>
</div>
</section>
<!-- ============================ Listings End ================================== -->
<?php
}
}
Thanks for the help in clarifying

Update Parent post type in child theme

I'm developing a child theme of a premium template, this comes with a custom post type with the label name of "release" but I'd like to change it to something else, I know that if I go to the functions.php file of the main theme I can change it easily but I'd like to change it from my child theme?
here is the code
<?php
add_action('init', 'kentha_release_register_type');
if(!function_exists('kentha_release_register_type')){
function kentha_release_register_type() {
$labelsrelease = array(
'name' => esc_html__("Release",'kentha'),
'singular_name' => esc_html__("Release",'kentha'),
'add_new' => esc_html__("Add new",'kentha'),
'add_new_item' => esc_html__("Add new release",'kentha'),
'edit_item' => esc_html__("Edit release",'kentha'),
'new_item' => esc_html__("New release",'kentha'),
'all_items' => esc_html__("All releases",'kentha'),
'view_item' => esc_html__("View release",'kentha'),
'search_items' => esc_html__("Search release",'kentha'),
'not_found' => esc_html__("No releases found",'kentha'),
'not_found_in_trash' => esc_html__("No releases found in trash",'kentha'),
'menu_name' => esc_html__("Album releases",'kentha')
);
$args = array(
'labels' => $labelsrelease,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'page',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 40,
'page-attributes' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-controls-play',
'supports' => array('title', 'thumbnail','editor', 'page-attributes' )
);
if (function_exists('ttg_custom_post_type')){
ttg_custom_post_type( "release" , $args );
}
/* ============= create custom taxonomy for the releases ==========================*/
$labels = array(
'name' => __( 'Release genres','kentha' ),
'singular_name' => esc_html__( 'Genre','kentha' ),
'search_items' => esc_html__( 'Search by genre','kentha' ),
'popular_items' => esc_html__( 'Popular genres','kentha' ),
'all_items' => esc_html__( 'All releases','kentha' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => esc_html__( 'Edit genre','kentha' ),
'update_item' => esc_html__( 'Update genre','kentha' ),
'add_new_item' => esc_html__( 'Add New genre','kentha' ),
'new_item_name' => esc_html__( 'New genre Name','kentha' ),
'separate_items_with_commas' => esc_html__( 'Separate genres with commas','kentha' ),
'add_or_remove_items' => esc_html__( 'Add or remove genres','kentha' ),
'choose_from_most_used' => esc_html__( 'Choose from the most used genres','kentha' ),
'menu_name' => esc_html__( 'Music genres','kentha' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
);
if(function_exists('ttg_custom_taxonomy')){
ttg_custom_taxonomy('genre','release',$args );
}
$fields = array(
array( // Repeatable & Sortable Text inputs
'label' => esc_html__( 'Release Tracks','kentha' ), // <label>
'desc' => esc_html__( 'Add one for each track in the release','kentha' ), // description
'id' => 'track_repeatable', // field id and name
'type' => 'repeatable', // type of field
'sanitizer' => array( // array of sanitizers with matching kets to next array
'featured' => 'meta_box_santitize_boolean',
'title' => 'sanitize_text_field',
'desc' => 'wp_kses_data'
),
'repeatable_fields' => array ( // array of fields to be repeated
'releasetrack_mp3_demo' => array(
'label' => esc_html__( 'MP3 Demo','kentha' ),
'desc' => esc_html__( '(Never upload your full quality tracks, someone can steal them)','kentha' ), // description
'id' => 'releasetrack_mp3_demo',
'type' => 'file'
),
'releasetrack_track_title' => array(
'label' => esc_html__( 'Title','kentha' ),
'id' => 'releasetrack_track_title',
'type' => 'text'
),
'releasetrack_artist_name' => array(
'label' => esc_html__( 'Artists','kentha' ),
'desc' => esc_html__( '(All artists separated bu comma)','kentha' ), // description
'id' => 'releasetrack_artist_name',
'type' => 'text'
),
'releasetrack_buy_url' => array(
'label' => esc_html__( 'Track Buy link','kentha' ),
'desc' => esc_html__( 'A link to buy the single track','kentha' ), // description
'id' => 'releasetrack_buyurl',
'type' => 'text'
),
'icon_type' => array(
'label' => esc_html__( 'Track icon (cart icon is default)','kentha' ),
'id' => 'icon_type',
'type' => 'select',
'default' => 'cart',
'options' => array(
array('label' => 'cart','value' => 'cart'),
array('label' => 'download','value' => 'download')
)
),
)
)
);
$fields_links = array(
array( // Repeatable & Sortable Text inputs
'label' => esc_html__( 'Custom Buy Links','kentha' ), // <label>
'desc' => esc_html__( 'Add one for each link to external websites','kentha' ), // description
'id' => 'track_repeatablebuylinks', // field id and name
'type' => 'repeatable', // type of field
'sanitizer' => array( // array of sanitizers with matching kets to next array
'featured' => 'meta_box_santitize_boolean',
'title' => 'sanitize_text_field',
'desc' => 'wp_kses_data'
),
'repeatable_fields' => array ( // array of fields to be repeated
'custom_buylink_anchor' => array(
'label' => esc_html__( 'Custom Buy Text','kentha' ),
'desc' => esc_html__( '(example: Itunes, Beatport, Trackitdown)','kentha' ),
'id' => 'cbuylink_anchor',
'type' => 'text'
),
'custom_buylink_url' => array(
'label' => esc_html__('Custom Buy URL','kentha' ),
'desc' => esc_html__( '(example: http://...)','kentha' ), // description
'id' => 'cbuylink_url',
'type' => 'text'
)
)
)
);
$fields_release = array(
array(
'label' => esc_html__('Main artist', "kentha"),
'id' => 'releasetrack_artist',
'type' => 'post_chosen',
'posttype' => 'artist'
),
array(
'label' => esc_html__( 'Label','kentha' ),
'id' => 'general_release_details_label',
'type' => 'text'
),
array(
'label' =>esc_html__( 'Release date (YYYY-MM-DD)','kentha' ) ,
'id' => 'general_release_details_release_date',
'type' => 'date'
),
array(
'label' => esc_html__( 'Catalog Number','kentha' ),
'id' => 'general_release_details_catalognumber',
'type' => 'text'
)
);
$details_box = new custom_add_meta_box( 'release_details', 'Release Details', $fields_release, 'release', true );
$sample_box = new custom_add_meta_box( 'release_tracks', 'Release Tracks', $fields, 'release', true );
$buylinks_box = new custom_add_meta_box( 'release_buylinkss', 'Custom Buy Links', $fields_links, 'release', true );
}}
I need to update in Child Teme the icon_type options adding this extra fields
array('label' => 'spotify','value' => 'spotify')
Any suggestion?
Thanks in advance

wp_query custom post types

I dont know if I just can t see the forrest for the tress, but I need some help. I have a custom post type "business" with 2 related custom taxonomies, business_category and business_tags. On the custom archive template I have a custom search form:
<form class="search-form" role="search" method="get" action="<?php echo home_url( '/' ); ?>">
<div class="form-group">
<label for="search-input"><i class="fa fa-search" aria-hidden="true"></i><span class="screen-reader-text">Search icons</span></label>
<input type="hidden" name="post_type" value="wego_business" />
<input type="search" class="form-control search-field" placeholder="Search the Directory" value="" name="s" id="s">
</div>
</form>
This then display on a custom search template. I can not get results for the business_tags
global $wp_query;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$tag = get_search_query();
$args = array (
's' => $s,
'order' => 'ASC',
'orderby' => 'name',
'tax_query' => array(
array(
'taxonomy' => 'business_tags',
'field' => 'slug',
'terms' => $tag,
),
),
'paged' => $paged
);
AM I missing something obvious?
Here is the cpt creation:
$labels = array(
'name' => _x( 'Business', 'Post Type General Name', 'wego_fran' ),
'singular_name' => _x( 'Business', 'Post Type Singular Name', 'wego_fran' ),
'menu_name' => __( 'Business', 'wego_fran' ),
'parent_item_colon' => __( 'Parent Business:', 'wego_fran' ),
'all_items' => __( 'All Business', 'wego_fran' ),
'view_item' => __( 'View Business', 'wego_fran' ),
'add_new_item' => __( 'Add New Business', 'wego_fran' ),
'add_new' => __( 'Add New', 'wego_fran' ),
'edit_item' => __( 'Edit Business', 'wego_fran' ),
'update_item' => __( 'Update Business', 'wego_fran' ),
'search_items' => __( 'Search Business', 'wego_fran' ),
'not_found' => __( 'Not found', 'wego_fran' ),
'not_found_in_trash' => __( 'Not found in Trash', 'wego_fran' ),
);
$args = array(
'label' => __( 'Business', 'wego_fran' ),
'description' => __( 'Business', 'wego_fran' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'page-attributes' ),
'taxonomies' => array( 'business_category', 'business_tags' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 20,
'menu_icon' => 'dashicons-admin-home',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'rewrite' => array( 'slug' => 'business' ),
);
register_post_type( 'wego_business', $args );
<?php
global $wp_query;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$tag = get_search_query();
$args = array (
'post_type' => 'wego_business',
's' => $s,
'order' => 'ASC',
'orderby' => 'name',
'tax_query' => array(
array(
'taxonomy' => 'business_tags',
'field' => 'slug',
'terms' => $tag,
),
),
'paged' => $paged
);
// The Query
$wp_query = new WP_Query( $args );
$count = $wp_query->found_posts;
?>
the custom search template is search-wego_business.php using the above query
Perhaps you're missing the post_type field and you can also search inside the name field. A refactor would look like:
$args = array (
'post_type' => 'business',
's' => $s,
'order' => 'ASC',
'orderby' => 'name',
'tax_query' => array(
array(
'taxonomy' => 'business_tags',
'field' => 'name',
'terms' => $tag,
),
),
'paged' => $paged
);
Else, I would need more details - like: your taxonomy and custom post type declaration and the full source of your archive (at least the part that builds the search query).

Custom taxonomy list in WordPress

I just registered following code in my function
add_action( 'init', 'create_resources_taxonomies', 0 );
function create_resources_taxonomies() {
$labels = array(
'name' => _x( 'cat2', 'taxonomy general name' ),
'singular_name' => _x( 'cat2', 'taxonomy singular name' ),
'search_items' => __( 'Search category' ),
'all_items' => __( 'All category' ),
'parent_item' => __( 'Parent category' ),
'parent_item_colon' => __( 'Parent category:' ),
'edit_item' => __( 'Edit category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New category Name' ),
'menu_name' => __( 'genre' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
);
register_taxonomy( 'genre', array( 'resources' ), $args );
}
and i want to get category list in my sidebar, so i added following code in my sidebar
<?php
$taxonomy = 'genre';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title
);
?>
<ul>
<?php wp_list_categories( $args ); ?>
</ul>
But i just get "No categories" text. What is wrong with my code?
Your code looks correct, I guess your categories are empty! Am i correct? if yes, add the following code to your last array section before 'title_li'
'hide_empty' => 0,
So your code should be like this
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'hide_empty' => 0,
'title_li' => $title

Drupal: how to make select list show taxonomy terms in hierarchy?

I have a block generated by Finder module (simple search mechanism). The block contains a form with select lists of various taxonomy dictionaries.
Their order, however seems quite random (neither, alphabetical, nor chronological) and lists regions in such an order with no hierarchy:
Italy, France, Poland, Benelux, ..., Upper Austria, ... Switzerland, Tyrol, Croatia
The order I expect is my dictionary hierarchy, so:
Austria, Austria>Tyrol, Austria>Upper Austria, Benelux, Croatia, France, Italy, Poland, Switzerland
Do You have any idea how to make the select list items appear in the dictionary hierarchy order?
Edit: Finder configuration:
array(
'finder_id' => '5',
'base' => 'views',
'title' => 'Search results',
'description' => 'Finder for search',
'path' => 'search',
'block' => '1',
'settings' => array(
'form_on_page' => 0,
'form' => array(
'prefix' => '',
'prefix_format' => '2',
'suffix' => '',
'suffix_format' => '2',
'button_text' => 'Find',
'go_text' => 'Go',
),
'advanced' => array(
'no_results' => array(
'no_results' => 'We can\'t find any tours. Try to search for other tours.',
),
'filter' => 1,
'submission' => 1,
'ahah' => 0,
'ahah_effect' => 'fade',
'element_combination' => '2',
'pager' => '0',
'match' => 'c',
'goto' => '',
'hide_args' => 1,
'arg_sep' => ',',
'empty_symbol' => '',
'show_links' => 1,
'show_admin_links' => 0,
'cache_finder_find' => '360',
'search' => '0',
'search_tab' => 'node',
),
'views' => array(
'view' => 'search_start',
'displays' => array(
'query' => 'default',
'output' => 'default',
),
'results' => '0',
'views_args' => array(
'args' => '',
'delimiter' => '/',
),
),
'i18nstrings' => array(
'title' => array(
'#i18nstrings' => 'finder:f5:title',
),
'description' => array(
'#i18nstrings' => 'finder:f5:description',
),
'settings' => array(
'form' => array(
'prefix' => array(
'#i18nstrings' => 'finder:f5:settings][form][prefix',
),
'suffix' => array(
'#i18nstrings' => 'finder:f5:settings][form][suffix',
),
'button_text' => array(
'#i18nstrings' => 'finder:f5:settings][form][button_text',
),
'go_text' => array(
'#i18nstrings' => 'finder:f5:settings][form][go_text',
),
),
'advanced' => array(
'no_results' => array(
'no_results' => array(
'#i18nstrings' => 'finder:f5:settings][advanced][no_results][no_results',
),
),
'pager' => array(
'#i18nstrings' => 'finder:f5:settings][advanced][pager',
),
'arg_sep' => array(
'#i18nstrings' => 'finder:f5:settings][advanced][arg_sep',
),
'empty_symbol' => array(
'#i18nstrings' => 'finder:f5:settings][advanced][empty_symbol',
),
'cache_finder_find' => array(
'#i18nstrings' => 'finder:f5:settings][advanced][cache_finder_find',
),
),
),
),
),
'elements' => array(
'0' => array(
'finder_element_id' => '13',
'finder_id' => '5',
'element' => 'select',
'title' => 'Region',
'weight' => '0',
'settings' => array(
'form' => array(
'description' => '',
'prefix' => '',
'prefix_format' => '2',
'suffix' => '',
'suffix_format' => '2',
'default_value' => '',
'required' => 0,
'size' => '',
'multiple' => 0,
'empty' => 'prepend',
'empty_text' => '- every region -',
),
'choices' => array(
'field' => array(
'term_data_1.name' => 'term_data_1.name',
),
'rewrite' => array(
'php' => '',
),
'displays' => array(
'query' => 'default',
),
'views' => array(
'args' => '',
'delimiter' => '/',
),
),
'advanced' => array(
'delimit' => '',
'value_combination' => '0',
),
'i18nstrings' => array(
'title' => array(
'#i18nstrings' => 'finder:e13:title',
),
'settings' => array(
'form' => array(
'description' => array(
'#i18nstrings' => 'finder:e13:settings][form][description',
),
'prefix' => array(
'#i18nstrings' => 'finder:e13:settings][form][prefix',
),
'suffix' => array(
'#i18nstrings' => 'finder:e13:settings][form][suffix',
),
'default_value' => array(
'#i18nstrings' => 'finder:e13:settings][form][default_value',
),
),
'advanced' => array(
'delimit' => array(
'#i18nstrings' => 'finder:e13:settings][advanced][delimit',
),
),
),
),
),
'element_handler' => array(
'#title' => 'Select list',
'#module' => 'finder_optionwidgets',
),
'#_finder_object' => '1',
),
'1' => array(
'finder_element_id' => '14',
'finder_id' => '5',
'element' => 'select',
'title' => 'Season',
'weight' => '1',
'settings' => array(
'form' => array(
'description' => '',
'prefix' => '',
'prefix_format' => '2',
'suffix' => '',
'suffix_format' => '2',
'default_value' => '',
'required' => 0,
'size' => '',
'multiple' => 0,
'empty' => 'prepend',
'empty_text' => '- every season -',
),
'choices' => array(
'field' => array(
'term_data_2.name' => 'term_data_2.name',
),
'rewrite' => array(
'php' => '',
),
'displays' => array(
'query' => 'default',
),
'views' => array(
'args' => '',
'delimiter' => '/',
),
),
'advanced' => array(
'delimit' => '',
'value_combination' => '0',
),
'i18nstrings' => array(
'title' => array(
'#i18nstrings' => 'finder:e14:title',
),
'settings' => array(
'form' => array(
'description' => array(
'#i18nstrings' => 'finder:e14:settings][form][description',
),
'prefix' => array(
'#i18nstrings' => 'finder:e14:settings][form][prefix',
),
'suffix' => array(
'#i18nstrings' => 'finder:e14:settings][form][suffix',
),
'default_value' => array(
'#i18nstrings' => 'finder:e14:settings][form][default_value',
),
),
'advanced' => array(
'delimit' => array(
'#i18nstrings' => 'finder:e14:settings][advanced][delimit',
),
),
),
),
),
'element_handler' => array(
'#title' => 'Select list',
'#module' => 'finder_optionwidgets',
),
'#_finder_object' => '1',
),
'2' => array(
'finder_element_id' => '15',
'finder_id' => '5',
'element' => 'select',
'title' => 'Theme',
'weight' => '2',
'settings' => array(
'form' => array(
'description' => '',
'prefix' => '',
'prefix_format' => '2',
'suffix' => '',
'suffix_format' => '2',
'default_value' => '',
'required' => 0,
'size' => '',
'multiple' => 0,
'empty' => 'prepend',
'empty_text' => '- every theme -',
),
'choices' => array(
'field' => array(
'term_data_3.name' => 'term_data_3.name',
),
'rewrite' => array(
'php' => '',
),
'displays' => array(
'query' => 'default',
),
'views' => array(
'args' => '',
'delimiter' => '/',
),
),
'advanced' => array(
'delimit' => '',
'value_combination' => '0',
),
'i18nstrings' => array(
'title' => array(
'#i18nstrings' => 'finder:e15:title',
),
'settings' => array(
'form' => array(
'description' => array(
'#i18nstrings' => 'finder:e15:settings][form][description',
),
'prefix' => array(
'#i18nstrings' => 'finder:e15:settings][form][prefix',
),
'suffix' => array(
'#i18nstrings' => 'finder:e15:settings][form][suffix',
),
'default_value' => array(
'#i18nstrings' => 'finder:e15:settings][form][default_value',
),
),
'advanced' => array(
'delimit' => array(
'#i18nstrings' => 'finder:e15:settings][advanced][delimit',
),
),
),
),
),
'element_handler' => array(
'#title' => 'Select list',
'#module' => 'finder_optionwidgets',
),
'#_finder_object' => '1',
),
'3' => array(
'finder_element_id' => '17',
'finder_id' => '5',
'element' => 'text',
'title' => 'Full text search',
'weight' => '3',
'settings' => array(
'form' => array(
'description' => '',
'prefix' => '',
'prefix_format' => '2',
'suffix' => '',
'suffix_format' => '2',
'default_value' => '',
'required' => 0,
'field_prefix' => '',
'field_suffix' => '',
'maxlength' => '',
'minlength' => '',
'size' => '',
'rows' => '0',
),
'choices' => array(
'field' => array(
'node_data_field_services.field_services_value' => 'node_data_field_services.field_services_value',
'node_data_field_subtitle.field_subtitle_value' => 'node_data_field_subtitle.field_subtitle_value',
'node_data_field_text.field_text_value' => 'node_data_field_text.field_text_value',
'node_data_field_tips.field_tips_value' => 'node_data_field_tips.field_tips_value',
'node_data_field_additional_services.field_additional_services_value' => 'node_data_field_additional_services.field_additional_services_value',
'node.title' => 'node.title',
),
'displays' => array(
'query' => 'default',
),
'views' => array(
'args' => '',
'delimiter' => '/',
),
),
'advanced' => array(
'delimit' => '',
'value_combination' => '0',
),
'i18nstrings' => array(
'title' => array(
'#i18nstrings' => 'finder:e17:title',
),
'settings' => array(
'form' => array(
'description' => array(
'#i18nstrings' => 'finder:e17:settings][form][description',
),
'prefix' => array(
'#i18nstrings' => 'finder:e17:settings][form][prefix',
),
'suffix' => array(
'#i18nstrings' => 'finder:e17:settings][form][suffix',
),
'default_value' => array(
'#i18nstrings' => 'finder:e17:settings][form][default_value',
),
),
'advanced' => array(
'delimit' => array(
'#i18nstrings' => 'finder:e17:settings][advanced][delimit',
),
),
),
),
),
'element_handler' => array(
'#title' => 'Text',
'#module' => 'finder_text',
),
'#_finder_object' => '1',
),
),
'elements_index' => array(
'13' => 0,
'14' => 1,
'15' => 2,
'17' => 3,
),
'base_handler' => array(
'#title' => 'Views finder',
'#module' => 'finder_views',
),
'admin_links' => array(
'search' => 'View \"Path\"',
'admin/build/finder/5/edit' => 'Edit',
),
'links' => array(),
'#_finder_object' => '1',
)
For your requirement drupal's Hierarchical Select module will help you..
You can do this really easily without a Drupal module if you're just theming:
$tree = taxonomy_get_tree($vid);
if ( $tree ) {
$content .= '<select class="your-class">';
$content .= '<option class="placeholder" value="">You Dropdown</option>';
foreach ($tree as $term) {
$path = taxonomy_term_path($term);
$content .= '<option value="/terms/' . strtolower(str_replace( " ", "-",$term->name)) . '">' . $term->name . '</option>';
}
$content .= '</select>';
}//end if ( $view )
return $content;
}
My solution: since I don't need much of Finder features, I came up with an alternative solution using just Views:
In the view listing all the nodes among which I'd like to search I added a filter Region:
Filters > Taxonomy > Term (The taxonomy term ID) > "regions" vocabulary
I exposed the filter, set it to dropdown and checked Show hierarchy in dropdown
Then I exposed the form into a block
4 And enabled the block under admin/build/block
It works fine for me. :-)

Resources