Get Wordpress Tags With 0 Posts Count - wordpress

I'm trying to get some tags from wordpress database, tags that aren't assigned to any posts, so their post count is 0.
The code i use selects only the tags with 1 or more posts assigned...
<?php
$args = array(
'smallest' => 12,
'largest' => 24,
'unit' => 'px',
'format' => 'flat',
'orderby' => 'name',
'order' => 'DESC',
'exclude' => null,
'include' => null,
'topic_count_text_callback' => 'default_topic_count_text',
'link' => 'view',
'taxonomy' => 'post_tag',
'echo' => true,
'child_of' => null,
'show_count' =>1
);
?>
How can i get the tags with 0 posts assigned to show?

Set hide_empty to false in the arguments of the get_tags function.
get_tags(array('hide_empty' => false));
If you're using custom taxonomy, use get_terms function.
get_terms('custom_tags', array('hide_empty' => false));

Related

Use ACF Taxonomy Field as Variable in Wordpress Custom Post Type Loop

I'm attempting to create a custom Wordpress query, using an Advanced Custom Fields field as a variable in the loop.
The use case is a page has a custom loop on it. For example, a page about a venue displays a loop of events (the custom post type) at the bottom of the page. I want for the person creating the page to choose what event tag (a tag taxonomy on the CPT) they want to attach to the page. Then the loop runs with that field attaching to the tag query used as a variable.
Here's my code so far:
<?php if ( get_field('event_tag') ) : ?>
<?php
$event_tag = get_field('event_tag');
$args = array(
'post_type' => 'events',
'posts_per_page' => 3,
'tag_id' => $event_tag,
'meta_key' => 'event_start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'ignore_sticky_posts' => true
);
?>
<?php echo $event_tag; ?><!-- This is only here to check the variable -->
<?php else : ?>
<?php
$args = array(
'post_type' => 'events',
'posts_per_page' => 3,
'meta_key' => 'event_start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'ignore_sticky_posts' => true
);
?>
<?php endif; ?>
<?php $secondquery = new WP_Query( $args );
if ( $secondquery->have_posts() ) : while ( $secondquery->have_posts() ) : $secondquery->the_post(); ?>
I'm still wanting to sort by the event date, thus the meta_key and orderby. I'm not sure if that's affecting this. A couple things to note:
• That temporary line echoing the $event_tag does output the tag id (in this case 31).
• I've tried wrapping that tag_id in '', echoing it, etc. But it just displays nothing.
• Because this is querying a custom post type, I'm not sure if the standard tag even works. The tag is registered like this...if it matters.
// Taxonomy / Tags
function taxonomies_events_tags() {
$args = array(
'hierarchical' => false,
'label' => __( 'Event Tags','taxonomy general name'),
'singular_name' => __( 'Event Tag', 'taxonomy general name' ),
'rewrite' => true,
'query_var' => true,
'show_in_rest' => true
);
register_taxonomy( 'custom-tag', 'events', $args );
}
add_action( 'init', 'taxonomies_events_tags', 0 );
What do I need to change in my query to get the events in the specified tag to show, still ordered by event_start_date?
Thanks in advance.
You need to use a tax query to get events from a certain category. Assuming the $event_tag variable contains the tag id for the taxonomy term, the following piece of code should work:
$args = array(
'post_type' => 'events',
'posts_per_page' => 3,
'meta_key' => 'event_start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'ignore_sticky_posts' => true,
'tax_query' => array(
array(
'taxonomy' => 'custom-tag',
'field' => 'term_id',
'terms' => $event_tag
)
)
);

Wordpress - display categories with subcategories of specific parent category

I want to display categories with nested subcategories of specific parent that I know the id of. I want to use wp_list_categories function. Here is my code:
$args = array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'show_count' => 0,
'hierarchical' => 1,
'title_li' => '',
'hide_empty' => 0,
'parent' => 20
);
wp_list_categories($args);
Categories of parent with the id of 20 are displaying correctly, but their children(subcategories) are not showing. What can be the solution here?
change: 'parent' => 20 to 'child_of' => 20

WooCommerce category and sub category loops

When building a WooCommerce site they have made it really easy to display categories and sub categories on the archive and category pages.
However does anyone know if it's possible to add a the list of categories/sub categories to the (content-single-product) page template?
I have a store that had only a handfull of products and we would like users to be able to quick select from the side menu rather than go back and forward between the archive page and the product pages.
Thanks to anyone who can help.
$prod_cat_args = array(
'taxonomy' => 'product_cat', //woocommerce
'orderby' => 'name',
'empty' => 0
);
$woo_categories = get_categories( $prod_cat_args );
foreach ( $woo_categories as $woo_cat ) {
$woo_cat_id = $woo_cat->term_id; //category ID
$woo_cat_name = $woo_cat->name; //category name
$return .= '' . $woo_cat_name . '';
}
$prod_cat_args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
you can also edit all fields try this

custom post type wp_dropdown_categories

How can i customize wp_dropdown_catagories to show custom posts. Let say posts created by woocommerce. Here is my code which shows blog posts. But I want custom posts in dropdown. How can I achieve that?
<?php $args = array(
'show_option_all' => 'All Catagories',
'show_option_none' => '',
'orderby' => 'ID',
'order' => 'ASC',
'show_count' => 1,
'hide_empty' => 0,
'child_of' => 0,
'exclude' => '1,5',
'echo' => 1,
'selected' => 0,
'hierarchical' => 0,
'name' => 'cat',
'id' => '',
'class' => 'postform',
'depth' => 1,
'tab_index' => 0,
'taxonomy' => 'category',
'hide_if_empty' => false,
); ?>
<?php wp_dropdown_categories( $args ); ?>
Resolved this. Added taxonomy of woocommerce in above code. Just replace value for taxonomy in product_cat.
'taxonomy' => 'product_cat',

How do you embed the Wordpress Categories widget in a page/post outside the sidebar?

Rather than use another widget I want to code the method to include the WP "Categories" widget directly within the content of a page or post.
Any ideas?
You can also use a plugin called Widgets on Pages and then just drag Categories into the newly created "sidebar" - then you just put the shortcode [widgets_on_pages] anywhere you want to use it.
To add wordpress post categories into the template file you need to use
<?php wp_list_categories($args); ?>
Default arguments are :
<?php $args = array(
'show_option_all' => ,
'orderby' => 'name',
'order' => 'ASC',
'show_last_update' => 0,
'style' => 'list',
'show_count' => 0,
'hide_empty' => 1,
'use_desc_for_title' => 1,
'child_of' => 0,
'feed' => ,
'feed_type' => ,
'feed_image' => ,
'exclude' => ,
'exclude_tree' => ,
'include' => ,
'hierarchical' => true,
'title_li' => __( 'Categories' ),
'show_option_none' => __('No categories'),
'number' => NULL,
'echo' => 1,
'depth' => 0,
'current_category' => 0,
'pad_counts' => 0,
'taxonomy' => 'category',
'walker' => 'Walker_Category' );
?>
or you can simply use it without parameters, in that case defaults will work.
<?php wp_list_categories(); ?>
you can read more about this function here
I think, you search for this.
https://wordpress.stackexchange.com/questions/36511/are-widgets-meant-to-be-used-outside-of-sidebars

Resources