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>';
?>
Related
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)
i have this code:
<?php
$taxonomyName = "free";
$terms = get_terms($taxonomyName,array('parent' => 0));
foreach($terms as $term) {
$term_children = get_term_children($term->term_id,$taxonomyName);
echo '<ul>';
foreach($term_children as $term_child_id) {
$term_child = get_term_by('id',$term_child_id,$taxonomyName);
echo '<li>' . $taxtitle . ' ' . $term_child->name . '</li>';
}
echo '</ul>';
}
?>
I want to count posts from each child taxonomy of this parent taxonomy.
I want to result something like this:
echo '<li>' . $taxtitle . ' ' . $term_child->name . ' (COUNT - Episodes)</li>';
Thank you.
it should be fairly simple :
<?php
$taxonomyName = "free";
$terms = get_terms($taxonomyName,array('parent' => 0));
foreach($terms as $term) {
$term_children = get_term_children($term->term_id,$taxonomyName);
echo '<ul>';
foreach($term_children as $term_child_id) {
$term_child = get_term_by('id',$term_child_id,$taxonomyName);
$q = new WP_Query(array(
'nopaging' => true,
'tax_query' => array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $term_child_id,
'include_children' => true,
)));
$count = $q->post_count;
echo '<li>' . $taxtitle . ' ' . $term_child->name . '(Season-'.$count.')</li>';
}
echo '</ul>';
}
?>
I'm trying to get all the tags that are within my custom post type "resource".
The problem is that I'm outside of the loop and struggling to find a way to get the functionality to work with the custom post type.
I have the category setup also as "resource_category"
My current code is:
$tax = 'post_tag';
$terms = get_terms( $tax );
$count = count( $terms );
if ( $count > 0 ): ?>
<div class="post-tags">
<?php
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, $tax );
echo '' . $term->name . ' ';
} ?>
</div>
<?php endif;
Can anyone help?
you are asking for the tags right, because the answer from 2015 is listing categories not tags
$args = array(
'type' => get_post_type(),
'orderby' => 'name',
'order' => 'ASC'
);
$tags = get_tags($args);
foreach($tags as $tag) {
var_dump($tag->name);
}
Tags are also WordPress taxonomy. So, you can get all the tags as like you get all terms
Read More
$tags = get_terms([
'taxonomy' => 'YOUR_CUSTOM_TAXONOMY',
'hide_empty' => false
]);
var_dump($tags);
You can also copy custom post taxonomy from Tags page URL.
http://localhost/wp-admin/edit-tags.php?taxonomy=YOUR_CUSTOM_POST_TAG_TAXONOMY_NAME&post_type=custom-post-type
$args = array(
'type' => 'resource',
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
}
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 )
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;}
}