Display number of posts from a certain category? - wordpress

How can I display ONLY the number of posts from a certain category ?
I want to display only the number without the category name.
I used this code
wp_list_categories('show_count=1&include=23&title_li=');
but it displays the category.
Any help please ??

I think it would help you to read this => http://wordpress.org/support/topic/display-number-of-posts-per-category
function number_postpercat($idcat) {
global $wpdb;
$query = "SELECT count FROM $wpdb->term_taxonomy WHERE term_id = $idcat";
$num = $wpdb->get_col($query);
echo $num[0];
}

Related

Trying to access order items details

I'm trying to write a function to display different custom thank you pages according to which product you purchased. The shop is simple, you can make a one-time donation or register for monthly donations to a non-profit. I want to display different thank you pages depending on whether which you did. I have several monthly donation products and one for one-time, separated into differnt categories. What I want to do is get the category of the products just purchased (one can only order either, not both, at a time), and use that to display the correct message.
function get_order_cat($order_id) {
$order = wc_get_order($order_id);
$items = $order->get_items();
print_r($items); // Returns the data, but it's protected !
}
This returns an array of objects, but the data is :protected. Any tips on how to access this, or get the result I'm looking for another way?
you can retrieve categories with that :
$order = wc_get_order($order_id);
foreach ($order->get_items("line_item") as $id_line_item => $item) {
$product_ID = $item->get_product_id();
$categories = get_the_terms($product_ID, "product_cat");
}

Wordpress custom SELECT query for alphabetical index

I'm trying to make an alphabetical index with wordpress, so far I've managed to get the $letter parameter and make the query, but I can't sort the results by a specific category_name, here's my code:
global $wpdb;
$results = $wpdb->get_results(
"
SELECT * FROM $wpdb->posts
WHERE post_title LIKE '$letter%'
AND post_type = 'directory'
AND category_name = 'company';
"
);
Everything works just fine untill the query gets to the category_name, I checked the Wordpress Codex but I didn't get it very well.
I would appreciate any help, thanks!

Can't display Wordpress categories which are assigned to a private post

I have a single post which is private. I have assigned a category to it, but now I'm not able to display categories using the get_categories() function.
The category page on wp-admin shows that no post is assigned to the category.
How do I get around this?
I found this code on WordPress Forum that does very similar.
I modified it to your needs:
global $wpdb;
$post_type_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_status = 'private'");
if($post_type_ids){
$post_type_cats = wp_get_object_terms( $post_type_ids, 'category',array('fields' => 'ids') );
if($post_type_cats){
$post_type_cats = array_unique($post_type_cats);
$post_type_cats = implode(',',$post_type_cats);
$cats=get_categories('include='.$post_type_cats);
}
}

Total number of comments of an author posts

I wanna display total number of comments that other users submitted for all posts of an author! for example something like this :
get_author_posts_total_number_of_comments($author->ID);
any idea ?
function get_author_posts_total_number_of_comments($authorID)
{
global $wpdb;
$sql = "SELECT SUM(comment_count) AS total FROM $wpdb->posts WHERE post_author=%d";
return $wpdb->get_var($wpdb->prepare($sql,$authorID));
}

How do I get product category information using collections in Magento

I am trying to output all the products from our Magento shop - the following code works, however I also need to grab the category id & the parent category name too. Can anyone suggest how I can do this?
$product = Mage::getModel('catalog/product');
$productCollection = $product->getCollection()
->addAttributeToSelect('*');
foreach ( $productCollection as $_product ) {
echo $_product->getName().'<br/>';
}
In some instances $_product->getCategory() can return empty and cause an error.
A better solution is to fetch categories by ID:
$categoryIds = $_product->getCategoryIds();
foreach($categoryIds as $categoryId) {
$category = Mage::getModel('catalog/category')->load($categoryId);
echo $category->getName();
echo $category->getUrlPath();
}
Since products can be assigned to multiple categories, I think your concept may be a bit off unless you are loading a collection for each category. What do you anticipate seeing if there are multiple categories for a given product?
Regardless, from within a category page, you can use the following:
$currentCat = $_product->getCategory();
To get all categories to which this product belongs:
$categories = $_product->getCategoryCollection();
foreach($categories as $_category) {
// do something
}
Hope that helps. Thanks,
Joe

Resources