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));
}
Related
I have searched stackoverflow. none of them apply to my question. Most of the answer just about limit per page (how many posts to show per page, not how many result to search)
I'm trying to speed up wordpress search.
For example I have 10millions post to search. and user type "a". It will take very long to search all of that because wordpress is using SQL_CALC_FOUND_ROWS.
The fact is that there is no need to search milions of rows (posts) and the answer "1000+" is enough for users. So we need to stop search after we found 1000 results.
The only way i can think of is to use:
'no_found_rows' => true
But then we dont have pagination functionality.
Or maybe we can use a separate query to get the result and to count how many posts found (with limiter)?
I dont understand how can wordpress doesnt have this functionality. Even big site like amazon limit the search result. So if you search "a" or "usb", it will return just the first 20 pages.
Try this code, its works on my wordpress with 1M posts
If you use search for custom post type, change
post_type = 'post'
to your post type name
add_filter('pre_get_posts', 'fix_search_pagination_with_nfr', 1000);
function fix_search_pagination_with_nfr() {
global $wp_query, $wpdb;
if ( ! is_admin() && $wp_query->is_main_query() && $wp_query->is_search() ) {
$wp_query->query_vars['no_found_rows'] = 1;
$wp_query->found_posts = $wpdb->get_var( "SELECT COUNT(*) FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')" );
$wp_query->found_posts = apply_filters_ref_array( 'found_posts', array( $wp_query->found_posts, &$wp_query ) );
if($wp_query->query_vars['posts_per_page'] > 0) {
$wp_query->max_num_pages = ceil($wp_query->found_posts / $wp_query->query_vars['posts_per_page']);
} else {
$wp_query->max_num_pages = 0;
}
}
return $wp_query;
}
I try to get the related order (the order id) from one of the line items id.
One case for example:
When editing an order, and deleting a line item (done by Ajax) the Woocommerce only provide the hook "woocommerce_before_delete_order_item" and passes $item_id only. (The woo function performs sql and not WP enviroment). I need the "belonging" order id to make some action!
My solution so far, is to loop through all orders and compare the Items ID, and break and return order ID when the match occurs.
This is way to slow, or "bulky clumsy", when keeping 10000 orders and more alive in the current shop.
This does NOT work:
wp_get_post_parent_id ( $item_id )
But Im hoping there is a similar call, or do I need to make a DB SQL search? Any answer involving mySQL, please make it "copy and paste ready". Im great at PHP but dont handle or never write my own SQL.
Thanks for any help! Below is my solution so far:
$order_id = my_wc_get_order_from_item_id($item_id);
function my_wc_get_order_from_item_id($id) {
$orders = get_posts('post_type=shop_order&numberposts=-1&post_status=publish');
foreach($orders as $obj) {
$order = new WC_Order($obj->ID);
if ( count( $order->get_items('line_item') ) > 0 ) {
foreach($order->get_items('line_item') as $item_id => $item ) {
if($item_id == $id) $return_value = $obj->ID;
if(isset($return_value)) break;
}
}
unset($order);
if(isset($return_value)) break;
}
if(isset($return_value)) return $return_value;
else return 0;
}
Your approach didn't work because order items are not posts in the wp_posts table, so they can't have a post_parent.
However, all is not lost and this was easier than I expected considering that my SQL isn't that strong. Looking at the woocommerce_order_item table in the database you can see that order_id and order_item_id are both there:
so I borrowed a little SQL statement from WooCommerce and modified it to find the order_id given a particular order_item_id row. Let me know how that works.
function so_38286531_get_order_item_order_id( $item_id ) {
global $wpdb;
$order_id = $wpdb->get_var( $wpdb->prepare(
"SELECT order_id FROM {$wpdb->prefix}woocommerce_order_items
WHERE order_item_id = %d",
$item_id
) );
return $order_id;
}
Use the built-in woocommerce function wc_get_order_id_by_order_item_id($item_get_id) to get order ID
// define the woocommerce_before_delete_order_item callback
function my_func_before_delete_order_item( $item_get_id ) {
$order_id = wc_get_order_id_by_order_item_id($item_get_id);
// do what you need...
};
// add the action
add_action( 'woocommerce_before_delete_order_item', 'my_func_before_delete_order_item', 10, 1 );
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!
How to made function to count total user registered on Wordpress site with just specific roles (eg: manager) and just users that registered current month only?
Example code, it will output user registered all time, I want it just ouput user registered on current month only.
function totalusertoday() {
global $wpdb;
$query = "SELECT COUNT(ID) FROM {$wpdb->prefix}users WHERE user_status = 0";
return $wpdb->get_var($query);
}
echo 'Total User Today: ' . totalusertoday();
Have a look at the Database Model:
http://codex.wordpress.org/images/9/97/WP3.8-ERD.png
To count all new users from this month, simply select all Users with registred Timestamp > then 1. day of this month.
To get only users with a specific role you have to select the user and the wp_usermeta, in wp_usermeta is a row called wp_capabilities.
http://wordpress.org/support/topic/name-of-database-fields-for-user-rolespermissions
Basically it's just a bit of sql query.
Solved! I'm just put filter for current month and year on user registered. And I've made simple code for this. Here is example code:
<?php
global $wpdb;
$date = getdate();
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users WHERE MONTH(user_registered) = $date[mon] AND YEAR(user_registered) = $date[year]" );
echo "<p>User count is {$user_count}</p>";
?>
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];
}