WordPress: Pagination for custom array - php

I have this snippet for creating a list of users sorted by the highest post views.
But I'm wondering of how can I create pagination for a custom array like that.
$topuser = array();
$avatar_size = 100;
$args = array(
'role__in' => array( 'contributor', 'author' ),
'hide_empty' => '1'
);
$users = get_users( $args );
foreach ( $users as $user ) {
$query = get_posts(
array(
'author' => $user->ID,
'cat' => '3',
'numberposts' => -1,
'post_type' => 'post',
)
);
$counter = 0;
$post_count = count_user_posts( $user->ID );
if ( ! $post_count ) {
continue;
}
// get each post of a user
foreach ( $query as $post ){
$views = absint( get_post_meta( $post->ID, 'post_views_count', true ) );
$counter += $views;
}
$topuser[] = array(
'id' => $user->ID,
'views' => $counter,
);
wp_reset_query();
}
// function to sort array based on views count
function sortViews( $a, $b ) {
return $b['views'] - $a['views'];
}
usort( $topuser, 'sortViews' ); // sort the array
$output = $topuser;
Thanks in advance!

Related

Make WooCommerce products featured programmatically

I've tried:
update_post_meta( $product->ID, '_featured', 'true');
But it didn't seem to work
I'm seeing that that removed this as the way to update the featured status of products in WooCommerce but can't find how to do it now
Im trying to get all of my featured and non featured dokan sellers and then update all of their products as featured or non featured based on their store featured status, through something like this:
$args = array( 'featured' => 'yes' );
$featured = dokan_get_sellers( $args );
$args = array( 'featured' => 'no' );
$not_featured = dokan_get_sellers( $args );
foreach ( $featured['users'] as $seller ) {
$products_f = get_posts( array(
'post_type' => 'product',
'author' => $featured->ID,
'posts_per_page' => -1
) );
}
foreach ( $not_featured['users'] as $seller ) {
$products_nf = get_posts( array(
'post_type' => 'product',
'author' => $not_featured->ID,
'posts_per_page' => -1
) );
}
foreach ( $products_f as $product) {
update_post_meta( $product->ID, '_featured', 'true');
}
foreach ( $products_nf as $product) {
update_post_meta( $product->ID, '_featured', 'false');
}
Current code:
add_action( 'set_featured_hook', 'set_featured' );
function set_featured(){
$args_featured = array( 'featured' => 'yes' );
$featured = dokan_get_sellers( $args_featured );
$args_nf = array( 'featured' => 'no' );
$not_featured = dokan_get_sellers( $args_nf );
foreach ( $featured['users'] as $seller ) {
$products_f = get_posts( array(
'post_type' => 'product',
'author' => $seller->ID,
'posts_per_page' => -1
) );
}
foreach ( $not_featured['users'] as $seller ) {
$products_nf = get_posts( array(
'post_type' => 'product',
'author' => $seller->ID,
'posts_per_page' => -1
) );
}
foreach ($products_f as $product) {
$wc_product_f = wc_get_product($product->ID);
$wc_product_f ->set_featured(1);
$wc_product_f ->save();
}
foreach ($products_nf as $product) {
$wc_product_nf = wc_get_product($product->ID);
$wc_product_nf->set_featured(0);
$wc_product_nf->save();
}
}
Thanks
foreach ($products_f as $product) {
$wc_product = wc_get_product($product->ID);
$wc_product->set_featured(1);
$wc_product->save();
}
foreach ($products_nf as $product) {
$wc_product = wc_get_product($product->ID);
$wc_product->set_featured(0);
$wc_product->save();
}
Use the built-in WooCommerce method set_featured() for updating the product.

Showing product view counter based on product attribute or categories WooCommerce

I used the below snippet to display a product view counter on the shop page based on the ip address and it worked fine.
add_action('wp', 'product_view_counter');
function product_view_counter() {
global $post;
$userip = $_SERVER['REMOTE_ADDR'];
if ( is_product() ){
$meta = get_post_meta( $post->ID, '_total_views_count', TRUE );
$meta = (!$meta) ? array() : explode( ',', $meta );
$meta = array_filter( array_unique( $meta ) );
if( ! in_array( $userip, $meta ) ) {
array_push( $meta, $userip );
update_post_meta( $post->ID, '_total_views_count', implode(',', $meta) );
}
}
}
Show on Shop page,
add_filter( 'woocommerce_loop_add_to_cart_link','show_product_view_counter_on_product_page',5);
function show_product_view_counter_on_product_page() {
global $product;
$id = $product->id;
$meta = get_post_meta( $id, '_total_views_count', true);
if(!$meta) {
$count = 0;
} else {
$count = count(explode(',',$meta));
}
echo '<div class="custom-visitor-count"><i class="fa fa-eye"></i><span class="counter-value">'.$count.' Views</span></div>';
}
The new idea is i want to display which 5 attributes or product categories have the most views on another page.
What I've done so far:
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids'
);
$get_data = get_posts($args);
foreach ($get_data as $gd){
$product = wc_get_product($gd);
$color = $product->get_attribute('pa_color');
$args_color = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'pa_color',
'field' => 'slug',
'terms' => $color,
)),
'fields' => 'ids',
);
$att_color = get_posts($args_color);
$att_color_sum = 0;
foreach($att_color as $ac){
$color = $product->get_attribute('pa_color');
$count_total_color = count($att_color);
$productid_color = $product->get_id();
$meta_color = get_post_meta( $productid_color, '_total_views_count', true);
$count_meta = count(explode(',',$meta_color));
$att_color_sum += $count_meta;
$display = $color." ".$att_color_sum;
}
}
echo "<p> Top 3 color attribute views : ".$display."</p>";
Output it gives a view like this :
Top 3 color attribute views : Blue 15
What i want is like this :
Top 3 color attribute views :
Green : 57
Red : 42
Grey : 24
Obviously, I was lost.
Anyone, please guide me so that I am on the right track.
Sincerely

I want to print the meta_value of all with the key "post_views_count" from the post_meta table

I want to print (meta_value) from all fields
"Post_views_count" from table (post_meta) But provided that this happens in every category.
I want to count every visit inaide selected category , and in the end appear the name of category and every category generate visitors inside it .
This is the code that i used it and work good but the page are too slowly due to recurrent queries
<?php
$args = array(
'orderby' => 'name',
'parent' => 0,
'depth' => 0
);
// Extract all categories
$categories = get_categories( $args );
foreach ( $categories as $category ) {
// Extract all posts within each category
$args = array(
'cat' => $category->term_id,
'orderby' =>'post_date',
'post_type' => 'post',
'posts_per_page' => '-1',
);
$query = new WP_Query( $args );
// Views variable
$allview_cat = 0;
if ( $query->have_posts() ) {
$posttype = get_post_type(get_the_ID());
while ( $query->have_posts() ) {
$query->the_post();
$do_not_duplicate[] = $post->ID;
// Query that extracts all fields containing post_views_count + post id
$post_id = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE (meta_key = 'post_views_count' AND post_id = $post->ID );");
$post_id = array_map(function($item){
return $item->{'meta_value'};
}
,$post_id);
foreach($post_id as $m_key => $m_value) {
$allview_cat = $m_value + $allview_cat ."<br>";
}
} // end while
} // end if
// Views variable
echo $allview_cat;
}
?>
<?php
$args = array(
'orderby' => 'name',
'parent' => 0,
'depth' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
$cat_id = $category->term_id;
$args_post = array(
'cat' => $cat_id,
'post_type' => 'post',
'posts_per_page' => '-1',
);
$query = new WP_Query( $args_post );
$allview = array();
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$post_id = get_the_ID();
if ( !isset( $allview[$cat_id] ) {
$allview[$cat_id] = 0;
}
$post_views_count = get_post_meta( $post_id, 'post_views_count' );
$allview[$cat_id] = (int)$allview[$cat_id] + (int)$post_views_count;
}
}
}
//display results
foreach ( $allview as $cat => $value ) {
echo $cat . ' = ' . $value;
}
?>

Query by part of string in custom field

I am trying to query for a string if present in a custom field.
e.g. Within the custom field I have a value like: Milano romani and If I have a string Milano from GET, I should find it. But the following gives me all posts even if no string match in the custom field
<?php
$myTerm = $_GET['cityName'];
$catIds = array();
$args = get_posts(
array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'usp-custom-23',
'value' => $myTerm,
'compare' => 'LIKE'
)
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$id = get_the_ID();
array_push($catIds, $id);
}
$catIds = implode( ", ", $catIds );
if(count($catIds) > 0){
$arrayFUllCity = "fullCity";
} else {
$arrayFUllCity = "empty";
}
}
var_dump($catIds);
?>

creation of submenu in wordpress plugin creation

I created a plugin and create a menu by using this code
add_filter('page_template', 'in_page_template');
function in_page_template()
{
global $wpdb;
$new_page_title = 'Packages';
$sql = "SELECT * FROM wp_posts where post_name='" . $new_page_title . "';";
$cnt_post = $wpdb->get_results($sql);
if (!(is_page('Home'))) {
$ppid = $_GET['page_id'];
if (count($cnt_post) != 0) {
$pid = $cnt_post[0]->ID;
if ($pid == $ppid) {
$page_template = dirname(__FILE__) . '/Packages.php';
return $page_template;
}
}
}
}
How can I create submenu for package page
add_action('init', 'create_initial_pages');
function create_initial_pages() {
$pages = array(
array(
'name' => 'post_name',
'title' => 'post_title',
'child' => array(
'page1-1' => 'National',
'page1-2' => 'International'
)
),
);
$template = array(
'post_type' => 'page',
'post_status' => 'publish',
'post_author' => 1
);
foreach( $pages as $page ) {
$exists = get_page_by_title( $page['title'] );
$my_page = array(
'post_name' => $page['name'],
'post_title' => $page['title']
);
$my_page = array_merge( $my_page, $template );
$id = ( $exists ? $exists->ID : wp_insert_post( $my_page ) );
if( isset( $page['child'] ) ) {
foreach( $page['child'] as $key => $value ) {
$child_id = get_page_by_title( $value );
$child_page = array(
'post_name' => $key,
'post_title' => $value,
'post_parent' => $id
);
$child_page = array_merge( $child_page, $template );
if( !isset( $child_id ) ) wp_insert_post( $child_page );
}
}
}
}

Categories