I am trying to exclude a category from wp_count_posts(). I've found hundreds of posts that explain how to do a WP_Query for a specific post type and get a category and then exclude posts with specific terms or tags.
However not very publicized is how to to exclude a few categories from a specific post type. Once I do that I need to return a post count as well
See below what I am working with. Looking for an alternative method for doing what I have below that will allow for me to exclude 3 specific categories.
<?php
$count_posts = wp_count_posts('sponsors');
$published_posts = $count_posts->publish;
$count = $published_posts/7;
$args = array(
'posts_per_page' => -1,
'post_type' => 'sponsors'
);
query_posts($args);
?>
<?php if( have_posts() ) : $i = 0; $f =7; $countr=0; $z = 0;?>
<div class="gallery-sponsors">
<strong class="title">Special Thanks To Our Sponsors</strong>
<div class="mask">
<div class="slideset">
<?php for ($k = 1; $k <= $count; $k++):
$countr = $countr+$f;?>
<?php if ($k==1):?>
<?php else:?>
<?php $i = $i+$f;?>
<?php endif;?>
<div class="slide">
<ul class="sponsors-list">
<?php $z=0;?>
<?php while (have_posts()) : the_post(); ?>
<?php $z++; if ($z<=$countr and $z>$i): ?>
<li>
<div class="img-hold">
<?php the_post_thumbnail( 'sponsors-gallery-home' ); ?><?php echo $z;?>
</div>
</li>
<?php endif;?>
<?php endwhile; ?>
</ul>
</div>
<?php endfor;?>
</div>
</div>
<nav class="pagination"><?php $k =0;?>
<ul>
<?php while ($q<$count-1): $q++;?>
<?php if ($q==1):?>
<li class="active"><?php echo $q;?></li>
<?php else:?>
<li><?php echo $q;?></li>
<?php endif;?>
<?php endwhile;?>
</ul>
</nav>
</div>
<?php endif;?>
<?php wp_reset_query(); ?>
Here's the fix. I had to add the following to the $args.
'cat' => '-71,-72,-73'
Pretty simple but these very little documentation on how to do this with wp_count_posts(). Here was my complete solution.
<?php
$count_posts = wp_count_posts('sponsors');
$published_posts = $count_posts->publish;
$count = $published_posts/7;
$args = array(
'posts_per_page' => -1,
'post_type' => 'sponsors',
'cat' => '-71,-72,-73'
);
query_posts($args);
?>
Related
I have custom post types 'stockist' and 'product', the latter having a taxonomy 'range'. Products are assigned to stockists using an ACF Post Object.
I'm trying to loop through 'stockist', return the 'range' term from the post object and then list the products for each term. So far I can loop all stockists and return all range terms however the foreach loop that returns the product list doesn't appear to be looping as it only returns the first set of values for each stockist.
Can anyone see where I'm going wrong?
<?php
$args2 = array( 'post_type' => 'stockist', 'posts_per_page' => -1 );
$stockistloop2 = new WP_Query( $args2 );
if ( $stockistloop2->have_posts() ): while ( $stockistloop2->have_posts() ): $stockistloop2->the_post();?>
<div class="col-1-1 clearfix nopad stockist-block-dropdown <?php the_title();?>-block">
<?php
$args = array( 'taxonomy' => 'range', 'hide_empty' => 0);
$categories = get_categories($args);
if($categories): foreach($categories as $category): $url = get_category_link( $category->term_id ); ?>
<div class="col-1-5">
<h4>
<?php echo ($category->name) ;?>
</h4>
<ul class="stockist-block-products clearfix">
<?php $post_objects = get_field('stocked_range'); if( $post_objects ):
foreach( $post_objects as $post): $post_terms_array = get_the_terms($post, 'range'); $post_term_name = $post_terms_array[0]->slug;
setup_postdata($post);
if($post_term_name == $category->slug):?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endif;
endforeach;
endif;?>
</ul>
</div>
<?php endforeach; ?>
<?php endif;?>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
UPDATE
Replacing the inner loop with the following has resolved the issue:
<?php $post_objects = get_field('stocked_range');
if( $post_objects ): ?>
<ul class="stockist-block-products clearfix">
<?php foreach( $post_objects as $post_object): $post_terms_array = get_the_terms($post_object, 'range'); $post_term_name = $post_terms_array[0]->slug;
if($post_term_name == $category->slug):?>
<li>
<?php echo get_the_title($post_object->ID); ?>
<span>Post Object Custom Field: <?php the_field('field_name', $post_object->ID); ?></span>
</li>
<?php endif; endforeach; ?>
</ul>
<?php endif;?>
I am rather new to php (dont get much further than tweaking some code)
So what i want is for every category (except category id 1, which is uncategorized)
I want the category url + category name
And for each post within the category, i want the url + title (loop max 3 times)
what i have so far is:
<div class="divs">
<div class="divs">
<div class="divs">
<h3 class="divs">
<a href="<?php echo get_category_link( "5" );?>">
<?php echo get_cat_name(5);?>
</a>
</h3>
</div>
<div class="divs">
<ol>
<?php
$args = array('category' => 5, 'post_type' => 'post');
$postslist = get_posts($args);
$i = 0;
foreach ($postslist as $post) : setup_postdata($post);{if(++$i > 3) break;}
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endforeach; ?>
</div>
</div>
</div>
</ol>
This does what i want perfectly, except it does it only for category id 5 ofcourse. Is there an easy way to make this loop for all categories?
Edit:
Basicly i want the code to look through all categories instead of just do it for category 5 (we add/remove categories, so hardcoding the numbers isn't usefull)
As you don't want to hardcode the category IDs, you need to fetch them through get_all_category_ids and loop over the result. This could work:
<div class="divs">
<div class="divs">
<?php
$categoryIds = get_all_category_ids();
foreach($categoryIds as $categoryId): ?>
<div class="divs">
<h3 class="divs">
<a href="<?php echo get_category_link($categoryId); ?>">
<?php echo get_cat_name($categoryId); ?>
</a>
</h3>
</div>
<div class="divs">
<ol>
<?php
$args = array('category' => $categoryId, 'post_type' => 'post');
$postslist = get_posts($args);
$i = 0;
foreach ($postslist as $post) : setup_postdata($post);
{
if (++$i > 3) {
break;
}
}
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endforeach; ?>
</ol>
</div>
<?php endforeach; ?>
</div>
</div>
This should work
<?php
$args = array( 'category__and' => array(2, 3, 4, 5), 'post_type' => 'post' );
$i = 0;
$postslist = get_posts( $args );
foreach ($postslist as $post) {
setup_postdata($post);
if (++$i > 4) break;
?>
<li><?php the_title(); ?></li>
<?php } ?>
So I have a php page in wordpress and when I add a part of the code it doesn't work. So to explain a bit more. I have a php template and most of it works, but when I add some additional code at the bottom that doesn't show up.
<?php get_header(); ?>
<div class="main_color container_wrap_first container_wrap">
<menu id="nav">
<ul><?php
$category_id = 2;
while( $category_id < 7 ) { ?>
<li>
<a href="<?php echo get_category_link( $category_id ); ?>">
<?php
$cat_id = $category_id;
echo get_cat_name( $cat_id );
$category_id++;
?>
</a>
</li>
<?php } ?>
</ul>
</menu>
<?php
$args = array(
'post_type' => 'projekti',
'posts_per_page' => -1,
'category' => '4',
);
$posts = get_posts($args);
if( $posts ):
$i = 1; ?>
<div class="custom-posts-grid">
<?php foreach($posts as $post): setup_postdata( $post ); ?>
<?php if( have_rows('logotipi') ): ?>
<?php while( have_rows('logotipi') ): the_row();
// vars
$image = get_sub_field('thumbnail_for_logotipi');
$content = get_sub_field('project_name');
$link = get_sub_field('url_logotipi');
$count = count($posts);
?>
<div class="post-grid-logotipi">
<a href="<?php echo $link; ?>">
<div class="post-title-hover"><?php echo $content ?></div>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" class="image-overlay-post" />
</a>
</div>
<?php
$i++;
if( $i == 6 ):
$i++;
?>
<div class="sodelujmo-post-grid"><div class="sodelujmo-post"><p class="sodelujmo-text">Vam je všeč,<br>kar vidite?<br>...<p><div class="button-bg"><a class="sodelujmo-link" href="../u3nek/sodelujmo/">Sodelujmo</a>
</div></div></div>
<?php
endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<!-- after this nothing I add can be shown -->
</div>
</div>
I have no idea what is wrong. So if anyone encountered anything similar before do let me know.
this is my code
<?php
$portfolio_categories = get_categories(array('taxonomy'=>'portfolio_category'));
foreach($portfolio_categories as $portfolio_category)
echo '<li data-filter=".' .$portfolio_category->slug. '">' .$portfolio_category->name. '</li> ';
?>
this shows the filters created by the category's name inside $portfolio_categories
then, there is the creation of client's list
<ul id="list" class="portfolio_list clearfix responsive">
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query( array( 'post_type' => 'portfolio', 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC' ) );
?>
<?php if (have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php $terms = wp_get_object_terms($post->ID, 'portfolio_category'); ?>
<?php $pf_bimg = wp_get_attachment_url( get_post_thumbnail_id() );?>
<?php $pf_simg = aq_resize( $pf_bimg, 420, 450, true ); ?>
<li class="span3 list_item <?php foreach ($terms as $term) { echo $term->slug.' '; } ?>">
<div class="recent-item">
<figure>
<div class="touching medium">
<img src="<?php echo $pf_simg; ?>" alt="<?php the_title(); ?>" />
<!-- <i class="icon-search"></i>
<i class="icon-link"></i> -->
</div>
<figcaption class="item-description">
<h5><?php the_title(); ?></h5>
<span><?php $i = 0; foreach ($terms as $term) { if($i)echo " / "; echo $term->name; $i=1; } ?></span>
</figcaption>
</figure>
</div>
</li>
<?php endwhile; ?> <?php endif; ?>
<?php wp_reset_query();?>
</ul>
What i 'm trying to do is to display a different element for every different category .
I thought to insert an if condition before the tag , like this
<?php
$portfolio_categories = get_categories(array('taxonomy'=>'portfolio_category'));
if ($portfolio_category = 'category1'): ?>
<figure>1</figure>
<?php else: ?>
<figure>2</figure>
<?php endif
?>
Thank all for your time,
Dan
You need to store last category, otherwise you'll have <figure> tag before each row:
<?php
$portfolio_categories = get_categories(array('taxonomy'=>'portfolio_category'));
?>
<?php if ($portfolio_category != $prev_portfolio_category): ?>
<?php if ($portfolio_category = 'category1'): ?>
<figure>1</figure>
<?php else: ?>
<figure>2</figure>
<?php endif; ?>
<?php endif; ?>
<?php
$prev_portfolio_category = $portfolio_category;
?>
My pagination code shows a full list of pages like 1,2,3,4,5,6,7,8,9,10 but i want it to show Prev,1,2,3,4,5,Next instead how can i do this with this code
if($tot>$perq) {
$output.="<div class='pagenation'>";
for($i=0;$i<($tot/$perq);$i++) {
$j=$i+1;
if($pg==$i)
$output.="<a href='".get_permalink()."?pg=$i' class='button active'>".$j."</a>";
else
$output.="<a href='".get_permalink()."?pg=$i' class='button'>".$j."</a>";
}
$output.="</div>";
}
Here is an example from a site I am working on. The keywords are 'previous_posts_link' and 'next_posts_link', which together with the $arg 'posts_per_page' will paginate for you.
Hope it helps.
<?php
the_content();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'bonsai','posts_per_page' =>12,'paged' => $paged);
$myposts = query_posts($args);
?>
<span id="tree-list-span">
<fieldset>
<legend>The Bonsai</legend>
<ul>
<?php
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li class="main_bonsai_list_item">
<a href="<?php echo get_permalink($post->id);?>">
<span class="grower_name"><?php the_title();?></span><br/>
<span class="grower_name">
<?php
$attr = array(
'id' => "link-".$post->ID,
'style' => 'max-width: 120px',
);
echo get_the_post_thumbnail($id,'bonsai-list-thumb',$attr);
?>
</span><br/>
<?php $custom = get_post_custom($post->ID);?>
<?php $grower = $custom["tree_grower"][0];?>
<span class="grower_name"><?php echo $grower;?></span>
</a>
</li>
<?php endforeach;
?>
</ul>
<span id="page_links">
<?php
previous_posts_link( '«' );
next_posts_link( '»', $myposts->max_num_pages );
wp_reset_postdata();
?>
</span>
</fieldset>
<?php
?>