Display Count of posts inside of a custom taxonomy term - wordpress

So, I have this page attached and have currently got a list outputting each Term where a post is contained in a particular term.
To do this I have a custom post type called 'episodes'
I have a custom Taxonomy called 'episode_categories'
and inside this I have various terms such as VR, Mixed Martial Arts and so on.
What I want to do is show a count of each term that has X amount of posts in it.
I currently have this as my code.
<div class="placeholder-main relative py-64 md:py-56 min-h-screen flex">
<div class="main my-auto w-full flex:none">
<div class="section">
<div class="container">
<div class="categories flex flex-wrap xs:block">
<?php
$args = array(
'type' => 'episodes',
'post_status' => 'publish',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'taxonomy' => 'episode_categories',
'pad_counts' => false
);
$categories = get_categories($args);
?>
<?php foreach ($categories as $category) : ?>
<div class="categories__item w-1/2 xs:w-full pr-5 xs:p-0 mt-10 xs:mt-6">
<div class="article text-2xl sm:text-xl xxs:text-base">
<h2 class="text-4xl sm:text-28px xxs:text-xl font-bold leading-none"><?php echo $category->name; ?></h2>
<p>There are
<strong>
0
</strong> podcasts in this category
</p>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
</div>

By default, the function get_categories returns the number of posts each category are associated to. You can access this value through category_count. Here is the code :
<p>
<strong>
<?= $category->category_count; ?>
</strong>
</p>
Tested and works

Related

WoCoommerce: get bundle data items product in custom loop

I have a pizza sales site which offers 'Ingredients' of which these are simple products, for example: Pizza Napolí is a bundle product, this product has ingredients grouped together.
What I'm looking for is to show in a custom loop (wp_query) the children products or ingredients of this pizza, which are 'Cheese(Queso)', 'Bacon(Tocineta)', etc.
Images reference here:
My code:
<?php
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => '_stock_status',
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array('pizzas'),
'field' => 'name',
'operator' => 'IN'
)
)
);
// End query
$products = new WP_Query( $args, 'woocommerce'); ?>
<div class="menu-grid-pizzas">
<?php if ($products->have_posts()): ?>
<div class="container">
<ul class="grid-pizzas-list">
<?php while ($products->have_posts()): $products->the_post(); ?>
<li class="grid-pizzas-content">
<div class="content-left">
<div class="grid-img">
<a href="<?php the_permalink();?>">
<?php the_post_thumbnail('medium');?>
</a>
</div>
</div>
<div class="content-right">
<div class="grid-product-title">
<a href="<?php the_permalink();?>">
<h4><?php the_title();?></h4>
</a>
</div>
<div class="grid-product-content">
<?php $content = get_the_content();?>
<p><?php echo mb_strimwidth($content, 0, 120, "...");?></p>
</div>
<div class="grid-product-ingredints">
<!-- HERE get DATA items bundles -->
</div>
<div class="grid-product-price">
<?php $precio = get_post_meta( get_the_ID(), '_price', true );?>
<p><?php echo wc_price( $precio ); ?></p>
</div>
<div class="grid-product-add-to-card">
<?php do_action('woocommerce_after_shop_loop_item' , 'woocommerce_template_loop_add_to_cart' , 10 );?>
</div>
</div>
</li>
<?php endwhile;?>
<?php wp_reset_postdata();?>
</ul>
</div>
<?php endif;?>
</div>
As you can see, it is a custom loop from which I call all products with the category 'Pizza' with a wp_query.
I hope you can help me, thank you.

How to display custom search results from a selected category of a custom post type?

I have a website that has a blog and a custom post type called proyecto.
They are on mywebpage.com/blog and mywebpage.com/portafolio.Each of those pages have a search field and category dropdown selector.
Each page should only search and filter for their respective posts. Also, each of them has a custom search result layout. If I'm on mywebpage.com/portafolio the search results should only display proyecto post type results with its custom layout. The same goes for when I only select a category from the dropdown (and dont type anything), it should only display posts type proyecto that have the category selected and only show the categories assigned to proyecto post types, again, with its own layout.
At the moment the search and category filter work (Im not even sure if it works properly) but it merges both of the results (or doesnt show them all) and only displays them with the post design layout... and when I look for an empty seach,it always displays all of the proyecto posts and some of the post results with the proyecto layout.
Here is my code on both page-blog.php and page-proyecto.php:
<section id="search">
<div class="container">
<div class="page-title">PAGE TITLE</div>
<form action="" method="post" class="search-form searchandfilter justify-content-between" id="searchbar">
<div class="row">
<div class="offset-lg-2 col-lg-6">
<label class="search-bar">
<div class="btn-search"><i class="fa fa-search"></i></div>
<input type="search" name="ofsearch" class="search-txt" placeholder="<?php echo esc_attr_x( 'Búsqueda', 'placeholder' ) ?>" value="<?php echo get_search_query()?>" />
<input type="hidden" name="post_type" value="post_type"/>
</label>
</div>
<div class="col-lg-4">
<?php
$categories = get_categories( array ('orderby' => 'name', 'order' => 'asc' ) );
if( $terms = get_categories( array('taxonomy' => 'category', 'orderby' => 'name') ) )
{
?>
<select name="ofcategory" id="ofcategory" class="postform custom-select" name="categoryfilter">
<option value="0">Categoría</option>
<?php
foreach ( $terms as $term )
{
?>
<option value="<?php echo $term->term_id;?>" data-categoria="<?php echo $term->name;?>"><?php echo $term->name;?></option>
<?php
}
?>
</select>
<?php
}
?>
<input type="hidden" name="ofcategory_operator" value="and">
</div>
</div>
<div class="row justify-content-end">
<input type="hidden" name="ofsubmitted" value="1">
<input type="submit" value="Filtrar" class="btn-submit btn-main-aqua">
<div class="btn-submit btn-main-aqua" onclick="displaymessage();reset()">Vaciar Campos</div>
</div>
</form>
</div>
</section>
NOTE:
This is the same code I use on both pages.
I only change the page title and value of the hidden input in the search.
I cannot know what the categories' names will be, they will be created by the user.
Here is my code on both search.php and category.php:
<?php get_header();?>
<section id="search">
<div class="container">
<div class="row m-0"><div class="page-title">Resultados de búsqueda</div></div>
<div class="row p-3"><div class="title">"<?php the_search_query();?>"</div></div>
<div class="row row-eq-height m-0">
<?php
if(have_posts())
{
if (get_post_type() == 'proyecto')
{
while (have_posts())
{
the_post();
$img = get_field("port-logo");
?>
<div class="col-lg-4 col-md-6 col-sm-12 p-0" data-aos="fade-up" data-aos-duration="1000">
<a href="<?php the_permalink();?>">
<div class="proyecto-img">
<img src="<?php echo $img;?>">
</div>
</a>
</div><!-- ITEM -->
<?php
}
}
else
{
if (get_post_type() == 'post')
{
while (have_posts())
{
the_post();
$titulo = get_field("blog-titulo");
$subtitulo = get_field("blog-subtitle");
$img = get_field("blog-img");
$intro = get_field("blog-intro");
?>
<div class="col-lg-4 col-md-6 col-sm-12" data-aos="fade-up" data-aos-duration="1000">
<div class="post-cont">
<a href="<?php echo the_permalink();?>">
<div class="post-img">
<img src="<?php echo $img;?>">
<div class="subtitle"><?php foreach((get_the_category()) as $category){ echo '- ' . $category->cat_name; }?></div>
</div>
</a>
<div class="desc-title"><?php echo the_title();?></div>
<div class="post-desc"><?php echo $intro;?></div>
Leer más
</div>
</div><!-- ITEM -->
<?php
}
}
else
{
?>
No results.
<?php
}
}
}
else
{
?>
No results.
<?php
}
?>
</div>
<div class="row p-0 m-0 mt-5">
<div class="btn-main-red">Regresar</div>
</div>
</div>
</section>
<?php get_footer();?>
Im not sure if this is the correct way to code it or if I should have 2 different search results documents for that...
My main problem is that they function as if they were 1 same search form instead of 2 different (because that code is on each page with different values on the hidden field), same goes to the search.php. Should I make 2 different files, if so how should i o it?
When I filter by category, I would like the url outcome to be something like this:
for the custom post type: mywebpage.com/proyecto/category/category1/?s=some_text
and for blog: mywebpage.com/category/category1/?s=some_text
I also tried using POST and GET and it did work but it makes my categories stop working...
I have been looking arround for an answer to my problem for days and even searched in the codex and various forums but I have not been successful, Im also a begginer so I dont know if I may have found the answer but I just didnt understand it... Any form of help adn guidance is apreciated, thanks.
Ok, here i can give you a clear idea that how to process... Firstly create a custom taxonomy for your CPT proyecto.. Eg proyecto_cat Create Taxonomy
Search Form Code
<form method="post" action="<?php echo site_url('search-results'); ?>">
<div class="row">
<select name="proyecto_cat">
<?php
$proyecto_cat = get_terms( array(
'taxonomy' => 'proyecto_cat',
'hide_empty' => false,
'orderby' => 'name'
) );
foreach ($proyecto_cat as $key => $value) {
?>
<option value="<?php echo $value->term_id; ?>"><?php echo $value->name; ?></option>
<?php } ?>
</select>
<input type="submit" name="proyecto_submit" value="Search" class="proyecto_submit">
</div>
</form>
Then Create a search-results.php template file and assign it to the page as we have mentioned in form action url.
search-results.php file
<?php
$proyecto_cat = $_POST['proyecto_cat'];
$tax_query = array(
array(
'taxonomy' => 'proyecto_cat',
'terms' => $proyecto_cat,
'field' => 'term_id',
'operator' => 'IN',
),
);
$args = array(
'post_type' => array( 'proyecto' ),
'order' => 'DESC',
'posts_per_page' => '-1',
'orderby' => 'date',
'tax_query' => $tax_query,
);
$query = new WP_Query( $args );
print_r($query);

Can't display custom post type categories on the page

I have a custom post type called "case_studies" and it has 3 categories: "seo", "website-design" and "facebook-advertising".
I have several posts in the main page, and for each post I want to output what categories the post has.
For example, the latest post has a category of "seo" and "facebook-advertising", and I want it to be displayed in the page.
I have tried the get_the_terms(); function but I think I am using it incorrectly.
Here is the code of the page:
<div class="container">
<!--
<div class="category_container">
<p class="category_item" id="all">All</p>
<p class="category_item" id="website">Websites</p>
<p class="category_item" id="facebook">Facebook Ads</p>
<p class="category_item" id="seo">SEO</p>
</div>
-->
<div class="row d-flex">
<?php
$args1 = array( 'post_type' => array('case_studies'), 'order' => 'DESC', 'posts_per_page' => 30, 'orderby' => 'date' );
$loop = new WP_Query( $args1 );
while ( $loop->have_posts() ) {
$loop->the_post();
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($loop->ID));
?>
<div class="col-sm-4">
<div class="case-study-content">
<a href="<?php the_permalink() ?>" class="blog_blocks">
<div class="b_image">
<img src="<?php echo get_the_post_thumbnail_url(); ?>"/>
</div>
<div class="b_h_sec">
<h2><?php the_title(); ?></h2>
<p><?php echo wp_strip_all_tags( get_the_excerpt(), true ); ?></p>
<span class="r_m">Read More</span>
</div>
</a>
</div>
</div>
<?php } ?>
<?php wp_reset_postdata(); ?>
</div>
</div>
Here is how the output looks right now and where I want categories to be displayed.

Wordpress not showing posts older than 2 days

I am recently experiencing a very odd bug.
I have created some custom fields for my posts and just realized that posts older than 2 days don't show anymore...if I change the date to yesterday or today, they reappear...
Here's the code I wrote to show my posts:
<?php
$partnersId=array();
$argSections = array(
'posts_per_page' => -1,
'post_type' => 'post',
'orderby' => 'date',
'category' => '38',
'order' => 'DESC'
);
$postsSections = get_posts($argsSections);
foreach ($postsSections as $postSections):
setup_postdata($postSections);
$idPartner = $postSections->ID; ?>
<div class="row box_agenda_filter" data-filter="<?php echo get_field('section_filter_',$idPartner); ?>">
<div class="event_time event_time_red">
<span><?php echo get_field('partner_type_',$idPartner); ?></span>
</div>
<div class="info_text pseudo_element">
<img src="<?php echo get_field('partner_logo_',$idPartner); ?>" style="width:200px; height:auto; position:relative; margin:20px 0;"/>
<div class="clear"></div>
<?php echo get_the_content($idPartner); ?>
</div>
</div>
<?php endforeach; ?>

wordpress outputting extra html code

I'm building a theme in WordPress, I'm a newbie, along with the image HTML is being outputted, please do help me resolve this issue, thank you.
<?php
$args = array( 'numberposts' => 4, 'order'=> 'ASC', 'orderby' => 'title', 'category' => '5' );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<li>
<div class="timeline-image">
<a href="<?php the_permalink(); ?>">
<img class="rounded-circle img-fluid" src="<?php echo the_post_thumbnail(); ?>">
</a>
</div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4 class="subheading text-left"><?php the_title(); ?></h4>
</div>
<div class="timeline-body">
<p class="text-muted text-justify"><?php the_excerpt(); ?>
Read More >>
</p>
</div>
</div>
</li>
<?php endforeach; ?>
You should be looping through the posts as such and not using foreach:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 4,
'orderby' => 'title',
'order' => 'ASC',
'category__in' => 5
);
$loop = new wp_query( $args );
while( $loop->have_rows() ) : $loop->the_row();
?>
Your article content…
<h4 class="subheading text-left"><?php the_title(); ?></h4>
<?php endif; ?>
If you need to loop through a different custom post types, use this guide.

Resources