WordPress plugin- counting posts - wordpress

My code was working properly, but accidentally I deleted something, but I don't remember what. At this moment my plugin display only one, last user. Before everything was ok and plugin could display all users. Do you have any ideas? Thanks in advance
function my_count_posts_by_user(){
global $wpdb;
$result = count_users();
$total_users = $result['total_users'];
for($id = 1;$id<=$total_users;$id++){
$result = $wpdb->get_results("SELECT wp_users.ID, wp_users.display_name, COUNT(wp_posts.post_author) AS 'Number_of_posts' FROM wp_users INNER JOIN wp_posts ON wp_users.ID = wp_posts.post_author WHERE wp_posts.post_type = 'post' AND wp_users.ID = $id AND wp_posts.post_status = 'publish'" , ARRAY_A);
}
echo '<table>';
foreach ($result as $x){
echo'<tr>';
echo'<td>'.'ID: '. $x['ID']."</td>";
echo'<td>'.'User : '. $x['display_name'].'</td>';
echo'<td>'.'Number of posts :'. $x['Number_of_posts'].'</td>';
echo'</tr>';
}
echo '</table>';
echo '<br>';

I think you should use WP API functions which will do everything for you.
get_users() function will get all users data just define fields which u require.
get_user_meta() function will get user meta data.
A small example
$users = get_users( array( 'fields' => array( 'ID' ) ) );
foreach( $users as $user_id ) {
print_r( get_user_meta( $user_id->ID ) );
}

Try this code:
function my_count_posts_by_user(){
global $wpdb;
$result = count_users();
$total_users = $result['total_users'];
for($id = 1;$id<=$total_users;$id++){
$result = $wpdb->get_results("SELECT wp_users.ID, wp_users.display_name, COUNT(wp_posts.post_author) AS 'Number_of_posts' FROM wp_users INNER JOIN wp_posts ON wp_users.ID = wp_posts.post_author WHERE wp_posts.post_type = 'post' AND wp_users.ID = $id AND wp_posts.post_status = 'publish'" , ARRAY_A);
echo '<table>';
foreach ($result as $x){
echo'<tr>';
echo'<td>'.'ID: '. $x['ID']."</td>";
echo'<td>'.'User : '. $x['display_name'].'</td>';
echo'<td>'.'Number of posts :'. $x['Number_of_posts'].'</td>';
echo'</tr>';
}
echo '</table>';
echo '<br>';
}
}

Related

Bring colors of SKUS products that are similar Woocommerce

I am listing similar sku on the product page and would like to get their colors as well.
E.g;
1-) Sku code : 21.127.21 Color : Red
2-) Sku code : 21.127.23 Color : Blue
3-) Sku code : 21.127.24 Color : Black
I probably need to update my query but I'm not sure
Following my code: functions.php
function wc_get_products_sku_img( $sku_excerpt ) {
global $wpdb;
// Get all product Ids and skus (standard objects) from a sku excerpt
return $wpdb->get_results( $wpdb->prepare( "
SELECT p.ID as id, pm.meta_value as sku
FROM {$wpdb->prefix}posts p
INNER JOIN {$wpdb->prefix}postmeta pm
ON p.ID = pm.post_id
WHERE p.post_type = 'product'
AND p.post_status = 'publish'
AND pm.meta_key = '_sku'
AND pm.meta_value LIKE '%s'
", '%'.$sku_excerpt.'%' ) );
}
global $product;
$results = wc_get_product_skus('21.127.');
foreach ( $results as $result ) {
$product_id = $result->id;
$product_sku = $result->sku;
$product = wc_get_product( $product_id );
echo 'Sku Code :'. $product_sku;
}
Thanks in advance
I figured out what the problem is, I'd like to share the solution in case someone needs it.
I joined the color property in my result value and called it.
global $product;
$results = wc_get_product_skus('21.127.');
foreach ( $results as $result ) {
$product_id = $result->id;
$product_sku = $result->sku;
$product_color = $result->pa_color;
$product = wc_get_product( $product_id );
echo 'Sku Code :'. $product_sku;
echo 'Sku Color :'. $product_color;
}

WordPress How can I get post_id from thumbnail_id?

I'm developing wordpress plugin.
I need to find out post_id from thumbnail_id(not reverse !).
How can I do this?
You can get result by this code
global $wpdb;
$_thumbnail_id = {thumbnail id};
$sql = "SELECT `post_id` FROM `wp_postmeta` WHERE `meta_value` = $_thumbnail_id";
$result = $wpdb->get_results( $sql, ARRAY_A );
//access first returned post id
var_dump($result[0]['post_id']);
If you added same image for multiple posts there will be multiple returns.
You can use get_the_ID() to get the post id. You can find this function in wp-includes/post-template.php
function get_the_ID() {
$post = get_post();
return ! empty( $post ) ? $post->ID : false;
}

Check if post_content already exist in database

I need a function to check if post_content already exist in database.
Wordpress built in function post_exists() checks by post post_title.
I need to check by post_content regardless of the post_title.
There such a function exist?
How can I solve this?
Thank you for your help
It looks like a small variation on post_exists() should work. Create a function like this in your child theme's functions.php, and then use it instead of post_exists():
function post_exists_by_content($content) {
global $wpdb;
$post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
$query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
$args = array();
if ( !empty ( $content ) ) {
$query .= ' AND post_content = %s';
$args[] = $post_content;
}
if ( !empty ( $args ) )
return (int) $wpdb->get_var( $wpdb->prepare($query, $args) );
return 0;
}

How to convert existing code into WooCommerce Plugin

Im extremely new to WordPress and WooCommerce code. I've been provided with some working code that resides in the plugins/woocommerce/templates/archive-product.php
The function is pretty simple, it simply fetches for an array of data from a remote site and makes use of the JSON returned to find the matching SKUs and inject them as items in the product list.
Works quite nicely, however, as I'm new to Woo & WP, I'm hoping someone might be able to show me how I can transform this code into the proper way of it being defined as a plugin?
I'm hoping its just a case of wrapping some additional code around the function, but I'm unsure as to where to start
any tips greatly appreciated
if ( wc_get_loop_prop( 'total' ) ) {
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$cat = explode('/', $actual_link);
if ($cat[4] == 'my-product-list') {
global $current_user;
get_currentuserinfo();
$data = array( 'email' => $current_user->user_email);
$response = wp_remote_post( 'https://www.shop.com/remote-data/', array( 'data' => $data ) );
$curl = 'https://www.shop.com/remote-data/';
$response = wp_remote_get( $curl );
$rows = wp_remote_retrieve_body( $response ) ;
$decode = json_decode(stripslashes($rows), true);
global $wpdb;
$product_id = array();
$i = 0;
foreach ($decode as $single_data) {
foreach ($single_data['items'] as $data) {
$result = $wpdb->get_results ( "SELECT post_id FROM wp_postmeta WHERE meta_key = '_sku' AND meta_value = '".$data."'" );
$product_id[$i] = $result[0]->post_id;
$i++;
}
}
$product_id = array_filter(array_unique($product_id));
$args = array(
'post_type' => 'product',
'post__in' => $product_id
);
//The Query
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) {
$the_query->the_post();
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
You could try to hook it into woocommerce_before_main_content
Like so:
function fetch_shop_data() {
// your code
}
add_action('woocommerce_before_main_content', 'fetch_shop_data');

How can I get a post by title in Wordpress?

Wordpress 3.0
I want to have the contents of a specific post into a page by using the title of the post. As far as I can tell, I can't do it directly with get_post().
I can assume what the brute force way might be, but I suspect there's a more elegant way?
get_page_by_title($id, OBJECT, 'post');
There ye go.
<!--1.Get post ID by post title if you know the title or the title variable-->
<?php
$posttitle = 'post_title';
$postid = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" . $posttitle . "'" );
echo $postid;
?>
<!--2.use get_post($post_id) to get whatever you want to echo-->
<?php
$getpost= get_post($postid);
$postcontent= $getpost->post_content;
echo $postcontent;
?>
No need to SQL query's when you can use wordpress own functions for this.
$page = get_page_by_title( 'Startsida' );
$page_id = $page->ID;
post_exists is a good function for that :
https://developer.wordpress.org/reference/functions/post_exists/
<?php
$post_id = post_exists('Your title');
// return id or 0 if post doesn't exists.
if($post_id>0)
get_post($post_id);
See my answer on a very similar question. Do not query the data base with an unescaped string.
You can use this:
1)
global $wpdb;
$your_title = "yourtitle";
$id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = $your_title");
echo $id;
or 2)
$slug_to_get = 'my_title_or_slug';
// you can use custom post type too
$posttypee='post';
$args=array(
'title' => $slug_to_get,
'post_type' => $posttypee,
'post_status' => 'publish'
);
$my_posts = get_posts($args);
if( $my_posts ) {
echo 'ID on the first post found '.$my_posts[0]->ID;
}

Resources