Order Foreach loop results - php

I cannot use the 'orderby' => 'post_count' as contributors have multiple posts assigned to them.
Rather, for each user output, I get their $post_count using the count_user_posts() function.
How can I re-order the items based on this number? Highest to lowest?
The user post count is not part of the $users array :(
// order contributors
$args = array(
'role' => contributors,
'orderby' => 'post_count',
'order' => 'ASC',
'fields' => 'all'
);
// The Query
$user_query = new WP_User_Query( $args );
$users = $user_query->get_results();
echo '<ul>';
$i = 0;
foreach ( $users as $user ) {
$post_count = count_user_posts( $user->id );
if ( count_user_posts( $user->id ) >= 1 ) {
echo '<li class="ws-sort" data-sort="' . $post_count . '"><a href="' . site_url() . '/author/' . $user->user_nicename . '">' . $user->display_name . '</li>';
if (++$i == 8) break;
}
}
echo '</ul>';

You can use usort with a custom function:
usort($users, function(&$a,&$b) {
if (!isset($a->_post_count)) $a->_post_count = count_user_posts( $a->id );
if (!isset($b->_post_count)) $b->_post_count = count_user_posts( $b->id );
return $a->_post_count < $b->_post_count;
});
And you can also use _post_count in the other foreach, since the sorting modified each element ( $a and $b are passed by reference )

Related

ACF Taxonomy only returning child - want both parent and child

I have the following function:
function my_acf_load_field( $field )
{
global $post;
global $current_user;
$field['choices'] = array();
wp_reset_query();
$query = new WP_Query(array(
'post_type' => 'gear',
'orderby' => 'title',
'order' => 'ASC',
'author' => $current_user->ID,
'posts_per_page' => -1,
));
foreach($query->posts as $product_id=>$macthed_product){
$zz = get_field('brand_preselect',$macthed_product->ID);
$yy = get_field('category_tax',$macthed_product->ID);
$aa = get_field('weight',$macthed_product->ID);
foreach( $yy as $term ):
$choices[$macthed_product->ID] = '<u class="cat">' . $term->name . '</u>' . '<i class="brand">' . $zz . '</i>' . $macthed_product->post_title . ' (' . $aa . ' kg)';
endforeach;
}
$field['choices'] = array();
if( is_array($choices) )
{
foreach( $choices as $key=>$choice )
{
$field['choices'][$key] = $choice;
}
}
// wp_reset_query();
return $field;
}
add_filter('acf/load_field/name=choices', 'my_acf_load_field');
This works well in that it populates the checkboxes with the category name, brand and title. For some reason $term->name in the foreach only shows the child category. If I echo out $term->name before the $choices[$macthed_product->ID] it shows all that apply. How do I get $term->name in the foreach to show all taxonomy matches (parent and child)

How to remove duplicate of user details

I am creating a filtering system to show all users within a Wordpress site (they have signed up via the User Registration Plugin).
I want to show a list of all users, filtering by their company. I have managed to call the company on each li by using:-
.str_replace( ' ', '-', $user->user_registration_company ) .
However I am struggling to do this for the buttons that actually filter, as it displays company names multiple times. Please see full code below:-
<div class="networking-list">
<?php
$args = array(
'role' => 'subscriber',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users( $args );
foreach ( $users as $user ) {
echo
'<button class="filter" data-filter="all">All</button>
<button class="filter" data-filter="all">'.str_replace( ' ', '-', $user->user_registration_company ) . '</button>';
}
$args = array(
'role' => 'subscriber',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users( $args );
echo '<ul>';
foreach ( $users as $user ) {
echo
'<li class="mix ' .str_replace( ' ', '-', $user->user_registration_company ) . '">' . get_avatar( $user->ID, $size = 280 ) .'<h2 class="filter-txt">' . esc_html( $user->first_name ) . ' ' . esc_html( $user->last_name ) .'</h2><h3></li>';
}
echo '</ul>';
?>
</div>
So I basically want the button to show the user company, but only once so I can filter.
Thank you!
The function you are searching for is array_unique
Just load all companies into an array and run the function on it. All duplicates will be removed and you are good to go.
<?php
$companies = array();
$args = array(
'role' => 'subscriber',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users($args);
foreach ($users as $user)
{
$companies[] = $user->user_registration_company;
}
$companies = array_unique($companies);
foreach ($companies as $company)
{
echo '<button class="filter" data-filter="all">All</button>
<button class="filter" data-filter="all">' . $company . '</button>';
}
$args = array(
'role' => 'subscriber',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users($args);
echo '<ul>';
foreach ($users as $user)
{
echo '<li class="mix ' . str_replace(' ', '-', $user->user_registration_company) . '">' . get_avatar($user->ID, $size = 280) . '<h2 class="filter-txt">' . esc_html($user->first_name) . ' ' . esc_html($user->last_name) . '</h2><h3></li>';
}
echo '</ul>';
?>

How to return an array of courses and rooms where the room capacity is greater than or equal to the course enrollment

I am struggling with how to return an array of courses that includes an array of rooms with a capacity that is greater than or equal to the course enrollment of each course.
The following code returns an array in the structure I want, but only the last value. I have tried array_merge_recursive, and either get an error or only the last value again.
I created the arrays like this:
global $wpdb;
$course_query1 = new WP_Query( array( 'post_type' => 'courses', 'posts_per_page' => '-1' ) );
$courses = $course_query1->posts;
$course_array1 = array();
foreach($courses as $i => $course) {
$course_array1[$i]['course_name'] = $course->post_name;
$course_array1[$i]['cap'] = $course->course_enrolled;
$course_array1[$i]['cap_array'] = $course->room_matches;
}
$room_query1 = new WP_Query( array( 'post_type' => 'jost_rooms', 'posts_per_page' => '-1' ) );
$rooms = $room_query1->posts;
$room_array1 = array();
foreach($rooms as $i => $room) {
$room_array1[$i]['room_name'] = $room->post_name;
$room_array1[$i]['cap'] = $room->room_capacity;
}
And created the function like so:
function compare_capacities10($array1, $array2){
for($c = 0; $c < count($array1); $c++) {
foreach($array1[$c] as $ckey => $ccap) {
if ($ckey == 'cap') {
for($r = 0; $r < count($array2); $r++) {
foreach($array2[$r] as $rkey => $rcap) {
if (($rkey == 'cap') && ($rcap >= $ccap)){
//this structure is right but only returns last value
$result = array($ckey, $ccap, array($rkey, $rcap) );
}
}
}
}
}
}
return $result;
}
$capacity_array10 = compare_capacities10 ($course_array1,
$room_array1);
print_r($capacity_array10);
EDIT:
I'm adding the code below, because it's achieving the same thing as above, but is better in WordPress because I can add other key-values to the display:
$courses = new WP_Query( array( 'post_type' => 'courses', 'posts_per_page' => '-1' ) );
if ( $courses->have_posts() ) {
while ( $courses->have_posts() ) {
$courses->the_post();
$enr = get_post_meta(get_the_ID(), 'course_enrolled' , true);
$cname = get_the_title();
$rooms = new WP_Query(
array(
'posts_per_page' => -1,
'post_type' => 'rooms',
'meta_query' => array(
array(
'key' => 'room_capacity',
'posts_per_page' => '-1',
'value' => $enr,
'compare' => '>='
)
)
)
);
if ( $rooms->have_posts() ) {
while ( $rooms->have_posts() ) {
$rooms->the_post();
$rname = get_the_title();
$cap = get_post_meta(get_the_ID(), 'room_capacity' , true);
echo 'cname: ' . $cname . ' ' . 'enrolled: ' . $enr . 'rname: ' . $rname. 'capacity: ' . $cap . '<br/>';
}
}
}
} else {
echo "Loop not working";
}
wp_reset_postdata();
I think you'll want to use array_push there.
Here's a link to the doc.
Right now you're overwriting the $result variable each iteration.

how to display wordpress categories in two columns?

I'd like to display my categories 2 at the time - 2 in each row and have as many rows as needed.
I've got so far with this code (which displays all the categories):
<?php
$i = 1;
$args = array(
'orderby' => 'name',
'parent' => 0,
'hide_empty' => 0
);
$categories = get_categories( $args );
echo count( $categories );
echo "<div class='a'>\n";
foreach ( $categories as $category ) {
echo "<div class='b'>\n";
echo "<div class='c'><a href='" . get_category_link( $category->term_id ) . "'>" . $category->name . "</a></div>\n";
echo "</div>\n";
}
echo "</div>\n";
?>
I know it can be done using list, but this is something I want to avoid.
So this is it (took a while) and it works just fine ;-)
$args = array(
'orderby' => 'name',
'parent' => 0,
'hide_empty' => 1
);
$categories = get_categories( $args );
$list_cats = array();
foreach ( $categories as $category ) {
array_push($list_cats, $category->name);
}
$total_cats=count($categories);
echo "<div class='a'>";
for ($a=0; $a<$total_cats;) {
for ($j=0; $j<2; $j++) {
echo " <div class='b'><div class='c'>" . $list_cats[$a] . "</div></div>";
$a++;
}
echo "</div>";
if($a<$total_cats){
echo "<div class='a'>";
}
}

Limit # in array

I have a script that displays users who have published on my site.... Im wondering how I can set the limit that is shown to 10.. I'm new to PHP.. Would I be able to use a foreach to achieve this?
<?php
// Display the widget title
if ( $title ) {
echo $before_title . $title . $after_title;
}
$args = array(
'role' => $role,
'orderyby' => 'post_count',
'order' => 'DESC'
);
$user_ids = get_users($args);
foreach ($user_ids as $user_id) {
if ($postcount) {
if(count_user_posts($user_id->ID)>0) {
echo '<a class="cuda-gravatar" href="'.get_author_posts_url($user_id->ID).'" title="'.$user_id->display_name.'">';
echo get_avatar($user_id->ID, $size);
echo '</a>';
} else {
}
} else {
echo '<a class="cuda-gravatar" href="'.get_author_posts_url($user_id->ID).'" title="'.$user_id->display_name.'">';
echo get_avatar($user_id->ID, $size);
echo '</a>';
}
}
You may try this (number):
$args = array(
'role' => $role,
'orderyby' => 'post_count',
'order' => 'DESC',
'number' => 10 // <-- add this
);
You may also use offset, read more on Codex about get users().
The classic way would be to set a limit to your sql query
$args = array(
...
'posts_per_page' => 10
);
or your can add a counter
foreach ($user_ids as $user_id) {
$i++
...
if($i==10){break;}
}

Categories