Wordpress post count for category including trashed - php

I've got this working nicely but I need to do something similar on another template. This code lists all the categories and shows how many posts are contained in each:
<?php
$categories = get_categories('exclude=1&order=DESC');
foreach ($categories as $cat) { ?>
<div class="cats">
<h2><?php echo $cat->cat_name; ?></h2>
<p>There are <?php echo $cat->category_count; ?> posts in this category.</p>
</div>
<?php }
?>
Now what I need to do is the same thing but the numbers output need to also include the posts that have been trashed.
Taking just one category as an example - let's call it 'Phones', let's say it has 5 published posts and 3 trashed posts, then the number returned would need to be 8.
I hope I explained that properly.
Thanks!

It is not that hard, however, you have to go about it in a slightly different way.
In foreach you should call either get_posts or WP_Query, to grab all the posts in a particular category with customized parameter post_status; then dispaly count of it.
In WP_Query you might use
['post_status' => array('publish', 'trash', 'inherit', ...)]
get_posts as
('post_status=any&categor=1&...')
EDIT
Here is a way, how to accomplish that.
<?php
$categories = get_categories('exclude=1&order=DESC');
foreach ($categories as $cat) {
$posts = new WP_Query( array('post_type' => 'post', 'post_status' => array('trash', 'publish'), 'cat' => $cat->cat_ID));?>
<div class="cats">
<h2><?php echo $cat->cat_name; ?></h2>
<p>There are <?php echo $posts->post_count; ?> posts in this category.</p>
</div>
<?php } ?>

$count = $wpdb -> get_results("SELECT count(*) as count
FROM `wp_posts`, `wp_term_relationships`
WHERE id=object_id AND term_taxonomy_id=$cat_id AND post_type='post' AND
post_status='publish'");
$count = $count[0] -> count;

Related

Multiple post loops in Wordpress

I've built a site for a health practitioner with CPT of:
(single-injuries.php, single-services.php, single-testimonials.php, single-partners.php)
and I have the appropriate (archive-injuries.php, archive-services, archive-testimonials, archive-partners) created with loops to display the relevant posts.
HOWEVER
I now want to create a sitemap page that pulls ALL THE POSTS from ALL ARCHIVES and just displays the PAGE NAME and URL for each...
How do I loop through multiple archives, do I nest a loop for each within a loop?
You can use a custom query that queries all of your CPTs which you listed (put them into the post_type array), similar to this (which lists all post titles found, each linked to its full post):
<?php
$args = array(
'post_type' => array('injuries', 'services', 'testimonials', 'partners' ),
'post_status' => 'publish',
);
$loop1 = new WP_Query($args);
if ( $loop1->have_posts() ) : while ( $loop1->have_posts() ) : $loop1->the_post();
$post_title = get_the_title();
?>
<div>
<p><a href='<?php echo get_the_permalink(); ?>'><?php echo post_title; ?></a></p>
</div>
<?php endwhile; else: ?>
<p>Nothing found</p>
<?php endif; ?>
I suggest you should do this using database, or what archives mean at your post? If you are using database just make a query that will select all from different tables, like this
SELECT archive-injuries.*, archive-services.*, archive-testimonials.*, archive-partners.* FROM your data base
Then make a while loop that will display posts, while mysqli_fetch_assoc have some data

show correct category name and fix post title permalink wordpress

I use this code to show different posts in different divs.
I have 2 problems:
I can't show the correct category name to current post.
The same category name is applied to 2 post when I use:
get_category_link($recent['ID'])
Clicking on the post title redirect me to home page instead post's page!
<div class="modulex">
<?php
$args = array('numberposts' => '1', 'post_status' => 'publish', 'offset' => '2');
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $recent) {?>
<div><?php echo get_the_post_thumbnail($recent['ID'],'small', array('class'=>'img-fluid')); ?></div>
<div class="spanlike"><h6><?php echo $recent["post_title"] ?></h6></div>
<?php } ?>
</div><?php
wp_reset_query();
?>
The problem with this is that get_category_link requires a category ID as argument, not post ID. To get around this, you'll have to perform several steps:
retrieve a list of categories for your post
decide which category to use from the list (assuming you only have one category per post, just use the first/only one)
get the link for this category
I would suggest using a custom function for this. Example below.
Here I think your problem is that get_permalink returns rather than echos. You could use the permalink instead:
<div class="spanlike"><h6><?php echo $recent["post_title"] ?></h6></div>
Example function for retrieving category link from post ID:
function get_cat_link_from_postID($postID) {
$categories = get_the_category($postID);
$catID = $categories[0]->term_id;
return get_category_link($catID);
}

How to get just one category and not all of them

Here is the problem:
I've been using this code to get other posts to display in the end of the page (as a list), and those posts should be in the same category as the post that a visitor is currently reading. It works fine, but the problem that occurs is when the post is assigned to multiple categories, then I have multiple listings which is not needed.
How can I restrict this , to just one category (by name, or by id or the first one), so that the loop shows me posts from just one category when post with multiple categories is displayed?
Many thx.
<?php global $post;
$categories = get_the_category();
foreach ($categories as $category) :
?>
<h3 class="naslovostalih">Ostali članci iz ove kategorije:</h3>
<ul class="clanciostalih">
<?php
$posts = get_posts('numberposts=10&category='. $category->term_id);
foreach($posts as $post) :
?>
<li>
<?php
$title = get_the_title($ID);
$link = get_permalink();
printf('<a class="linkpost" title="%s" href="%s">%s</a>', $title, $link, $title);
the_post_thumbnail('thumb-232');
echo '<div id="excerptcu">';
echo excerpt(25);
echo '</div>';
?>
<p class="more-link-wrapper2"><?php _e( 'Opširnije »', 'fearless' ); ?></p>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
You can add the category id to get_posts just as in your code
Instead of looping the categories try like below: here 3 is the category id
$posts = get_posts('category=3&numberposts=10');
Also if you want to retrieve the category id of specific category by name you can use
get_cat_ID( $cat_name )
Perhaps you could just take the first category in your foreach loop instead of iterating through all of them? You can do this by setting a variable like
$first = true;
Before your loop. Then, set your foreach loop in an "if" that checks to see if $first == false. If so, do the loop. In your loop, do your stuff and set $first = true and it will only iterate once.

How to determine category name of returned Posts in WordPress

What i'm trying to do is find out what category the Posts are when they are returned so i can put them in specific divs.
e.g. i have #div1 #div2 and #div3 and i have 3 posts returned each with a different category.
What i want to do is have 1 post each in a div. To do this i need to determine the category.
Here is how i fetch Posts by category but how do i fetch Posts of 3 categories and determine the category of the Posts returned, so i can change the div id?
<?php
//gallery
$args = array( 'category_name' => 'Clubs');
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post );
?>
<div id="div1">
<?php the_content(); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
Obviously i could duplicate this code twice and just change the category and div names but is that the right way to do this?
To avoid copy-paste, I'd suggest putting your code in a function, which takes the parts you want to change as parameters. Something like this (I added the posts_per_page parameter, since you said you only want one post per div):
<?php
function show_gallery($category, $id) {
$args = array( 'category_name' => $category, 'posts_per_page' => 1);
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post );
?>
<div id="div<?php echo $id; ?>">
<?php the_content(); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
}
show_gallery('Clubs', 1);
show_gallery('Hearts', 2);
show_gallery('Diamonds', 3);
?>
I haven't tested, but hopefully it works and is enough to get you started.
I'd put the function declaration at the top of your template.

WordPress: Display Other Posts From Current Category

I have a function going that displays all posts under the same custom taxonomy called "issue". I need to adjust it so that it further narrows it down to also only display posts under the same category.
I took a look at the WordPress get_the_category() function but didn't have much luck with that.
Here is the code:
<?php
$issueid = get_the_term_list( $post->ID, 'issue', '', ', ', '' );
$postslist = get_posts("numberposts=100&issue=$issueid");
foreach ($postslist as $post) :
setup_postdata($post); ?>
<div class="sidebar-box">
<div class="sidebar-left">
<p><?php the_title(); ?></p>
<p><?php the_date(); ?></p>
</div>
<div class="sidebar-right">
<?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>
</div>
</div>
<?php endforeach; ?>
This will properly show the current category id:
<?php
$category = get_the_category();
echo $category[0]->cat_id;
?>
So I tried editing the first batch of code to only show posts within the current category id but it still returns everything:
$category = get_the_category();
$categoryid = $category[0]->cat_id;
$issueid = get_the_term_list( $post->ID, 'issue', '', ', ', '' );
$postslist = get_posts("numberposts=100&issue=$issueid&category=$categoryid");
foreach ($postslist as $post) :
setup_postdata($post); ?>
This is the get_the_category function reference: http://codex.wordpress.org/Function_Reference/get_the_category
Any help would be greatly appreciated.
Thanks,
Wade
get_the_term_list() returns an html string, not ID's of related categories. So when you pass $issueid to get_posts(), you are including an html string, not an integer. I believe the reason you are getting all posts returned is because WP is ignoring that query var because it isn't what its expecting.
You want to use get_posts() and include the ID for 'issue' to get all posts assigned the category of 'issue'.
You want to use get_the_category() to get all categories related to a post.
Can you clarify if you want to show all posts under the same categories as the current post which is under the category 'issue'? Do you want to list the related posts right after the current post, or do you want to display ALL the related posts to ALL the 'issue' posts in the sidebar?

Categories