ACF Custom user field in search query - wordpress

I have created some field in user section, Now i want to search user by custom field. Like Phone of City Name. While search by first name and last name is working fine by could not get user by phone or city name.
My custom user field is "phone" and "pin_code"
<?php if( $_GET['s'] ) {
$search_term = sanitize_text_field( stripslashes( $_GET['s']));
// WP_User_Query arguments
$args = array (
'order' => 'ASC',
'orderby' => 'display_name',
'search' => '*' . esc_attr( $search_term ) . '*',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => $_phone,
'value' => $search_term,
'compare' => 'LIKE'
)
)
);
// Create the WP_User_Query object
$wp_user_query = new WP_User_Query( $args );
// Get the results
$authors = $wp_user_query->get_results();
// Check for results
if ( ! empty( $authors ) ) {
// loop through each author
foreach ( $authors as $author ) {
// get all the user's data
$author_info = $author->ID; ?>
<div ><?php
$phone = get_field('phone', 'user_'. $author_info );
$adderess = get_field('adderess', 'user_'. $author_info );
$adderess_2 = get_field('adderess_2', 'user_'. $author_info );
$zip = get_field('pin_code', 'user_'. $author_info );
$bio = get_field('coach_bio', 'user_'. $author_info );
$category = get_field('category', 'user_'. $author_info );
if( get_field('avatar', 'user_'. $author_info ) ) {
$avatar = get_field('avatar', 'user_'. $author_info );
}
else {
$avatar = 'http://0.gravatar.com/avatar/0c12cfa22b6d6f53d7701858ecc3b67e?s=96&d=mm&r=g';
}
?>
</div>
<div class="member">
<h2 class="member-name"><?php echo $author->display_name; ?></h2>
<div class="user">
<?php $user = get_userdata( $author_info );
$user_roles = $user->roles;
if ( in_array( 'pms_subscription_plan_11', $user_roles, true ) ) {
echo '<img src="http://lifecoachnearme.co.uk/wp-content/uploads/2020/01/policy-badge.png" class="badget">';
}
?>
<div class="article_left">
<?php echo '<img src="'.$avatar.'" >'; ?>
</div>
<div class="article_right">
<div class="profile-display-address">
<div class="col-6"><?php echo $adderess; ?></div>
<div class="col-6"><?php echo $adderess_2; ?></div>
<div class="col-6"><?php echo $zip; ?></div>
</div>
<div class="contact-form contact-links">Contact me</div>
</div>
<div class="button-profile"><a class="button whitebg" href="<?php echo get_author_posts_url($author_info); ?>">View profile</a></div>
</div>
<?php }
echo '</ul>';
}
else {
echo 'No authors found';
}
}
?>
I really done know how to pass custom user field in query arg.
Also i want to give a dropdown next to the search input that user can select there area of expertise. Like they are teacher or student. So result should be relative

Related

Display Users attached to Custom Post Type - ACF

I am trying to display users attached to a Custom Post Type. I created an ACF-Users-Field which returns the ID. This field is displayed at my Custom Post Type. Now I would like to display the names of the users selected in this field.
I am able to display all names of my users (if I remove the part 'meta_query' of my arguments, but if i add the meta_query array to my arguments nothing is displayed. I am not sure, maybe the mistake is the 'value', but I have no idea what to change there. Below I show the relevant code:
<article>
<?php
$args = array(
'role' => 'Subscriber',
);
$my_user_query = new WP_User_Query( $args );
$editors = $my_user_query->get_results();
if ( ! empty( $editors ) ) {
foreach ( $editors as $editor ) {
$editor_info = get_userdata( $editor->ID );
$args = array(
'post_type' => 'projekt',
'meta_query' => array(
array(
'key' => 'projektteilnehmer', // name of custom field - return ID
'value' => $post->ID ,
'compare' => 'LIKE'
)
)
);
// The User Query
$user_query = new WP_User_Query( $args );
// The User Loop
if ( ! empty( $user_query->results ) ) { ?>
<?php foreach ( $user_query->results as $user ) {
echo $user->user_firstname;;
}
} else {
echo 'nothing found';
}
} // endforeach
} else {
echo __( 'Kein Mitglied gefunden.' );
}
?>
</article>
Any help appreciated
By what your describing this code should do what you want. given that the participant's are returned as userID
<article>
<?php
$teilnehmers = get_field('projektteilnehmer', $post->ID);
if ( ! empty( $teilnehmers ) ) {
foreach ( $teilnehmers as $teilnehmer ) {
$user = get_user_by('ID', $teilnehmer);
if($user){
echo $user->user_firstname;;
}
}
} else {
echo __( 'Kein Mitglied gefunden.' );
}
?>
</article>
or if they are returned as user objects
<article>
<?php
$teilnehmers = get_field('projektteilnehmer', $post->ID);
if ( ! empty( $teilnehmers ) ) {
foreach ( $teilnehmers as $teilnehmer ) {
echo $teilnehmer->user_firstname;;
}
} else {
echo __( 'Kein Mitglied gefunden.' );
}
?>
</article>
The answer of Jasper B pushed me in the right direction. Thank you very much:) It is working with this query:
<?php // projekte
$projekte = get_posts(array(
'post_type' => 'projekt',
'meta_query' => array(
array(
'key' => 'projektteilnehmer', // name of custom field - return value id
'value' => $editor_info->ID,
'compare' => 'LIKE'
)
)
));
?>
<?php if( $projekte ): ?>
<strong>Projekte:</strong>
<ul>
<?php foreach( $projekte as $projekt ):?>
<li>
<a href="<?php echo get_permalink( $projekt->ID ); ?>">
<?php echo get_the_title( $projekt->ID ); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif;
// ende projekte ?>

Woocommerce Widget Custom Price with Thousand separator and Decimal separator

I am using a template which has a widget to show woocommetce product on the home page, Currently price of product is just a number E.g. ₹18500/- without any decimal or comma, I want to show price with Thousand separator and Decimal separator E.g. ₹ 18,500/-
Note: Thousand separator and Decimal separator is enabled from woocommerce settings, and here I want to show price on a widget.
I am attaching code of the widget.
any help will be appreciated.
* Filter the arguments for the Recent Posts widget.
*
* #since 1.0.0
*
* #see WP_Query
*
*/
$query_args = array(
'posts_per_page' => $post_number,
'post_status' => 'publish',
'post_type' => 'product',
'no_found_rows' => 1,
'order' => $order,
'meta_query' => array(),
'tax_query' => array(
'relation' => 'AND',
),
);
switch ( $wc_advanced_option ) {
case 'featured' :
if( !empty( $product_visibility_term_ids['featured'] )){
$query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => $product_visibility_term_ids['featured'],
);
}
break;
case 'onsale' :
$product_ids_on_sale = wc_get_product_ids_on_sale();
if( !empty( $product_ids_on_sale ) ){
$query_args['post__in'] = $product_ids_on_sale;
}
break;
case 'cat' :
if( !empty( $travel_way_wc_product_cat )){
$query_args['tax_query'][] = array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $travel_way_wc_product_cat,
);
}
break;
case 'tag' :
if( !empty( $travel_way_wc_product_tag )){
$query_args['tax_query'][] = array(
'taxonomy' => 'product_tag',
'field' => 'term_id',
'terms' => $travel_way_wc_product_tag,
);
}
break;
}
switch ( $orderby ) {
case 'price' :
$query_args['meta_key'] = '_price';
$query_args['orderby'] = 'meta_value_num';
break;
case 'sales' :
$query_args['meta_key'] = 'total_sales';
$query_args['orderby'] = 'meta_value_num';
break;
case 'ID' :
case 'author' :
case 'title' :
case 'date' :
case 'modified' :
case 'rand' :
case 'comment_count' :
case 'menu_order' :
$query_args['orderby'] = $orderby;
break;
default :
$query_args['orderby'] = 'date';
}
$travel_way_featured_query = new WP_Query( $query_args );
if ($travel_way_featured_query->have_posts()) :
echo $args['before_widget'];
$animation = "init-animate zoomIn";
?>
<section id="<?php echo esc_attr( $unique_id ); ?>" class="at-widgets acme-abouts <?php echo $bg_gray_class;?>">
<div class="container">
<?php
if ( ! empty( $title ) ) {
echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
}
$div_attr = 'class="featured-entries-col woocommerce"';
?>
<div class="row at-cat-product-wrap clearfix ">
<div <?php echo $div_attr;?>>
<?php
$travel_way_featured_index = 1;
while ( $travel_way_featured_query->have_posts() ) :$travel_way_featured_query->the_post();
$travel_way_list_classes = 'single-list';
if ( 1 == $column_number ) {
$travel_way_list_classes .= " col-sm-12";
} elseif ( 2 == $column_number ) {
$travel_way_list_classes .= " col-sm-6";
} elseif ( 3 == $column_number ) {
$travel_way_list_classes .= " col-sm-4 col-md-4";
} else {
$travel_way_list_classes .= " col-sm-4 col-md-3";
}
?>
<div class="<?php echo esc_attr( $travel_way_list_classes ); ?>">
<a href="<?php the_permalink();?>">
<?php the_post_thumbnail($travel_way_img_size)?>
<div class="caption">
<h3 class="at-woo-title"><?php the_title();?></h3>
<?php
woocommerce_template_loop_rating();
$currency = get_woocommerce_currency_symbol();
$price = get_post_meta( get_the_ID(), '_regular_price', true);
$sale = get_post_meta( get_the_ID(), '_sale_price', true);
if($sale) :
global $post, $product;
echo apply_filters( 'woocommerce_sale_flash', '<span class="onsale">' . esc_html__( 'Sale!', 'travel-way' ) . '</span>', $post, $product );
?>
<p class="product-price">
<del>
<?php
echo esc_html($currency);
echo esc_html( $price );
?>
</del>
<?php
echo esc_html($currency);
echo esc_html( $sale . '/-' );
?>
</p>
<?php elseif($price) : ?>
<p class="product-price">
<?php
echo esc_html($currency);
echo esc_html( $price . '/-');
?>
</p>
<?php endif;
?>
</div>
</a>
</div><!--dynamic css-->
<?php
$travel_way_featured_index++;
endwhile;
?>
</div><!--featured entries-col-->
</div><!--cat product wrap-->
<?php
echo $args['after_widget'];
echo "<div class='clearfix'></div>";
// Reset the global $the_post as this query will have stomped on it
?>
</div>
</section>
<?php
endif;
wp_reset_postdata();
}
} // Class Travel_Way_Wc_Products ends here
} ```
You can use number_format. Should be something like:
$price = floatval($price);
$decimal_separator = wc_get_price_decimal_separator();
$thousand_separator = wc_get_price_thousand_separator();
$decimals = wc_get_price_decimals();
$formatted_price = number_format( $price, $decimals, $decimal_separator, $thousand_separator );
echo esc_html( $formatted_price . '/-');

WordPress search is not working with more than 2 characters

I have created a custom template for search our products with lots of filters and one input field to search with characters. However, it works only with one or two characters like "a", "ag" but not with more than two characters. If I search for something like "pol" or "polar" it redirects me to WordPress 404 - PAGE NOT FOUND.
Update:
I have tried two solutions: one with the custom query ajax and that works fine with a search but the only problem I faced is with the pagination; the other one used WordPress standard query object, it works well with pagination but I'm getting a problem with search character length. Below are the two attempts that I tried.
Attempt #1: with the Ajax. this code is written down in function.php file
function get_search_results()
{
global $wpdb;
if(!empty($_POST['search_input'])) {
$search_input = $_POST['search_input'];
$where = 'pm.meta_value LIKE '. "'%".$search_input."%'".' AND pm.meta_key LIKE "%pattern_number"';
$where1 = 'p.post_title LIKE '."'%".$search_input."%'".' AND p.post_type = "product"';
$search_sql = 'SELECT DISTINCT(pm.post_id), ph.lightness, ph.color_label FROM wp_posts p
INNER JOIN wp_products_huecolor ph on p.ID = ph.post_id
INNER JOIN wp_postmeta pm ON p.ID = pm.post_id
WHERE p.post_status = "publish" AND p.post_type = "product" AND (('.$where.') OR ('.$where1.')) ORDER BY ph.color_label, ph.lightness ASC';
$total_query = "SELECT COUNT(1) FROM (${search_sql}) AS combined_table";
$total = $wpdb->get_var( $total_query );
$items_per_page = 10;
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$offset = ( $page * $items_per_page ) - $items_per_page;
$page = $wpdb->get_results( $search_sql.' LIMIT '.$offset.', '.$items_per_page );
//echo "SQL = ".$wpdb->last_query; wp_die();
echo '<ul>';
foreach ($page as $object)
{
// get custom fields values related to a post by post id
$fields = get_fields($object->post_id);
// get post details by post id
$post_detail = get_post($object->post_id, $object );
$brand_name = $fields[brand]->name;
$full_sheet_image = $fields[full_sheet_image][sizes][thumbnail];
$post_title = $post_detail->post_title;
$post_name = $post_detail->post_name;
?>
<li class="post-pattern">
<div class="search-productsBoxSecond">
<a target="_blank" href="<?php echo site_url( '/products/'.$post_name.'', 'http' ); ?>"><img src="<?php echo $full_sheet_image; ?>" alt="" class="vc_single_image-img attachment-full"></a>
<div class="search-productNameSecond"><a target="_blank" href="<?php echo site_url( '/products/'.$post_name.'', 'http' ); ?>"><?php echo $post_title; ?></a></div>
<div class="search-productBrandSecond"><?php echo $brand_name; ?></div>
</div>
</li>
<?php
}
echo '</ul>';
echo '<div class="search-product-navigation">';
echo paginate_links( array(
'base' => add_query_arg( 'cpage', '%#%', site_url( '/pattern-search/' ) ),
'format' => '',
'prev_text' => __('«'),
'next_text' => __('»'),
'total' => ceil($total / $items_per_page),
'current' => $page
));
echo '</div>';
}
exit();
}
add_action('wp_ajax_nopriv_filter_featured_products','filter_featured_products');
add_action('wp_ajax_filter_featured_products','filter_featured_products');
Attempt #2: with Standard WP_Query in the custom template.
<?php
$args = array();
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$search_keywords = ( get_query_var( 's' ) ) ? get_query_var( 's' ) : "";
if(isset($search_keywords) && $search_keywords != ''){
$args = array(
'post_type' => 'product',
'search_prod_title' => $search_keywords,
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged,
'wildcard_on_key' => true,
'meta_query' => array(
array(
'key' => 'product_type_%_pattern_number',
'value' => $search_keywords,
'compare' => 'LIKE',
),
)
);
}else{
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged
);
}
// query for getting featured product
add_filter( 'posts_fields', 'products_posts_select_filter', 10, 2 );
add_filter( 'posts_join', 'products_posts_join_filter', 10, 2 );
add_filter( 'posts_where', 'title_filter', 10, 2 );
add_filter( 'posts_orderby', 'products_posts_orderby', 10, 2 );
add_filter('posts_groupby', 'products_posts_groupby', 10, 2);
$sql_query = new WP_Query( $args );
//echo $wpdb->last_query; die();
//echo '<pre>'; print_r($sql_query); die;
//echo $sql_query->request; die;
remove_filter( 'posts_fields', 'products_posts_select_filter', 10, 2 );
remove_filter( 'posts_join', 'products_posts_join_filter', 10, 2 );
remove_filter( 'posts_where', 'title_filter', 10, 2 );
remove_filter( 'posts_orderby', 'products_posts_posts_orderby', 10, 2 );
remove_filter('posts_groupby', 'products_posts_groupby', 10, 2);
if( $sql_query->have_posts()):
echo '<ul>';
while( $sql_query->have_posts()): $sql_query->the_post();
{
//get custom fields meta values related to a post
$fields = get_fields($post->ID);
$full_sheet_image = $fields[full_sheet_image][sizes][thumbnail];
//echo'<pre>'; print_r($post_detail); die;
?>
<li class="post-pattern">
<div class="search-productsBoxSecond">
<a target="_blank" href="<?php echo site_url( '/products/'.$post->post_name.'', 'http' ); ?>"><img src="<?php echo $full_sheet_image; ?>" alt="" class="vc_single_image-img attachment-full"></a>
<div class="search-productNameSecond"><a target="_blank" href="<?php echo site_url( '/products/'.$post->post_name.'', 'http' ); ?>"><?php echo $post->post_title; ?></a></div>
<div class="search-productBrandSecond"><?php echo $fields[brand]->name; ?></div>
</div>
</li>
<?php
}
endwhile; ?>
</ul>
<!-- pagination -->
<div class="search-product-navigation">
<?php
echo paginate_links(array(
'total' => $sql_query->max_num_pages
));
?>
</div>
<?php endif; ?>
here are the filter functions thats written in the function.php
/*Replaced meta_key = with meta_key LIKE */
add_filter( 'posts_where', function ( $where, \WP_Query $q )
{
// Check for our custom query var
if ( true !== $q->get( 'wildcard_on_key' ) )
return $where;
// Lets filter the clause
$where = str_replace( 'meta_key =', 'meta_key LIKE', $where );
return $where;
}, 10, 2 );
function title_filter( $where, &$wp_query )
{
global $wpdb;
if ( $search_term = $wp_query->get( 'search_prod_title' ) ) {
$post_title_clause = '('.$wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $search_term ) ) . '%\') OR';
$where = substr_replace($where, $post_title_clause , 6, 0);
}
return $where;
}
function products_posts_select_filter( $fields, &$wp_query ){
$fields = 'DISTINCT(wp_posts.ID), wp_posts.post_title, wp_posts.post_name, ph.lightness, ph.color_label';
return $fields;
}
function products_posts_join_filter( $join, &$wp_query ){
$join .= " INNER JOIN wp_products_huecolor ph on wp_posts.ID = ph.post_id";
return $join ;
}
function products_posts_orderby( $orderby, &$wp_query ){
$orderby = 'ph.color_label, ph.lightness ASC';
return $orderby;
}
function products_posts_groupby( $groupby, &$wp_query ){
return $groupby = '';
}

Get grouped product highest price woocommerce

I am trying to get grouped products highest price so that i can show only highest price on custom template page.I am using WP_Query to get list of products.
Here is the snippet:
$series_term = $_GET['series'];
$args=array(
'series' => $term_name->slug,
'post_type' => 'product',
'posts_per_page' => 8,
'filter' =>$valuecol,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'hidden' ), // Don't display products in the knives category on the shop page
'operator' => 'NOT IN'
)
)
);
// $do_not_duplicate[] = $post->ID;
$my_query = null;
$my_query = new WP_Query($args);
/* Start the Loop */
while ($my_query->have_posts()) : $my_query->the_post();
$feat_prod_img = wp_get_attachment_image_src(get_post_thumbnail_id(), 'post-thumb-single');
$feat_product_img = wp_get_attachment_url( get_post_thumbnail_id($my_query->post->ID) );
?>
<div class="col-md-3 col-sm-4 col-xs-6">
<div class="product-detail">
<div class="product-thmbnial">
<a href="<?php the_permalink(); ?>">
<img src="<?php $src=$feat_product_img; echo image_product_resize_crop( $src, $w=440, $h=275, $dest = null, $override = false, $createNewIfExists = false ); ?>" alt="<?php the_title(); ?>"/>
</a>
</div>
<div class="info"><p><?php the_title(); ?><br/>
<span>
<?php $wp_product = new WC_Product( get_the_ID() );
echo get_woocommerce_currency_symbol(); ?></sup><?php
echo $wp_product->get_price();?></span></p></div>
</div>
</div>
<?php endwhile; ?>
I want grouped product highest price to show in place of $wp_product->get_price();
I tried to get children products and but the array is empty.
foreach ( $wp_product->get_children() as $child_id ) {
$child_prices[] = get_post_meta( $child_id, '_price', true );
}
can anyone suggest me how i can get highest price.
Thanks in advance.
I found the solution.. I tried the same solution earlier but that time it wasn't working.
Here is the fix:
$series_term = $_GET['series'];
$args=array(
'series' => $term_name->slug,
'post_type' => 'product',
'posts_per_page' => 8,
'filter' =>$valuecol,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'hidden' ), // Don't display products in the knives category on the shop page
'operator' => 'NOT IN'
)
)
);
// $do_not_duplicate[] = $post->ID;
$my_query = null;
$my_query = new WP_Query($args);
/* Start the Loop */
while ($my_query->have_posts()) : $my_query->the_post();
$feat_prod_img = wp_get_attachment_image_src(get_post_thumbnail_id(), 'post-thumb-single');
$feat_product_img = wp_get_attachment_url( get_post_thumbnail_id($my_query->post->ID) );
?>
<div class="col-md-3 col-sm-4 col-xs-6">
<div class="product-detail">
<div class="product-thmbnial">
<a href="<?php the_permalink(); ?>">
<img src="<?php $src=$feat_product_img; echo image_product_resize_crop( $src, $w=440, $h=275, $dest = null, $override = false, $createNewIfExists = false ); ?>" alt="<?php the_title(); ?>"/>
</a>
</div>
<div class="info"><p><?php the_title(); ?><br/>
<span>
<?php $wp_product = new WC_Product( get_the_ID() );
$child_prices = array();
foreach ( $product->get_children() as $child_id ) {
$child_prices[] = get_post_meta( $child_id, '_price', true );
}
$child_prices = array_unique( $child_prices );
if ( ! empty( $child_prices ) ) {
$min_price = min( $child_prices );
$max_price = max( $child_prices );
} else {
$min_price = '';
$max_price = '';
}
if($min_price == $max_price && !empty($min_price))
{
echo get_woocommerce_currency_symbol(); ?></sup><?php
echo $min_price;
}
elseif(!empty($max_price))
{
echo get_woocommerce_currency_symbol(); ?></sup><?php
echo $max_price;
}
else
{
echo get_woocommerce_currency_symbol(); ?></sup><?php
echo $wp_product->get_price();
}
?></span></p></div>
</div>
</div>
<?php endwhile; ?>

Get Wordpress child category from specific parent

I'm building a small thumbnail gallery with posts from a category that has the ID 406.
Some of the posts are in multiple categories and I'm not sure how to grab the category name that is a child of 406. $post_cat[0]->name; returns a category, but I only need it to return children of 406.
$thumbnails = get_posts('posts_per_page=10&cat=406');
foreach ($thumbnails as $thumbnail) {
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => 406,
);
$categories = get_categories ( $args );
foreach ( $categories as $category) {
$cat_name = $category->name;
$cat_slug = $category->slug;
}
echo $cat_name;
echo $cat_slug;
}
*EDIT**
$thumbnails = get_posts('posts_per_page=10&cat=406');
foreach ($thumbnails as $thumbnail) {
$post_cat = get_the_category( $thumbnail->ID );
echo '<li><a class="side-thumb" href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
if ( has_post_thumbnail($thumbnail->ID)) {
echo get_the_post_thumbnail($thumbnail->ID, 'thumbnail');
$upload_dir = wp_upload_dir();
$art_image = ''.$upload_dir['basedir'].'/icons/'.$post_cat[0]->slug.'.png';
if (file_exists($art_image)) {
echo '<p class="artist-latest"><img src="http://site.com/wp-content/uploads/icons/'.$post_cat[0]->slug.'.png" alt="'.$post_cat[0]->slug.'"/>'.$post_cat[0]->name.'</p>';
} else{
echo '<p class="artist-latest">'.$post_cat[0]->name.'</p>';
}
} else {
}
echo '</a></li>';
}
So now I'm fetching a list of category and its child, then I fetch posts having parent category assigned to it, after I get posts array, I loop into it and get all the categories assigned to the post, If there are additional categories other than the required category and its child I skip the post, otherwise do whatever I want to do.
$category = get_category_by_slug( 'category-name' );
$args = array(
'type' => 'post',
'child_of' => $category->term_id,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => FALSE,
'hierarchical' => 1,
'taxonomy' => 'category',
);
$child_categories = get_categories($args );
$category_list = array();
$category_list[] = $category->term_id;
if ( !empty ( $child_categories ) ){
foreach ( $child_categories as $child_category ){
$category_list[] = $child_category->term_id;
}
}
$posts_args = array(
'posts_per_page' => 10,
'cat' => $category->term_id,
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts = new WP_Query ( $posts_args );
if ( $posts->have_posts() ){
while ( $posts->have_posts() ){
$posts->the_post();
$category_array = array();
$post_categories = get_the_category ( get_the_ID() );
if ( !empty ( $post_categories ) ){
foreach ( $post_categories as $post_category ) {
$category_array[] = $post_category->term_id;
}
}
//Checks if post has an additional category
$result = array_diff( $category_array, $category_list );
if ( empty ( $result ) ) { ?>
<li>
<a href="<?php the_permalink(); ?>" class="side-thumb" title="<?php the_title(); ?>"> dfdf<?php
if ( has_post_thumbnail() ){
echo get_the_post_thumbnail();
}
//Put your default icon code here
?>
</a>
</li> <?php
}
}
}
wp_reset_postdata();
Above answer is as per your explanation, but I think what you want is, like you have a category Artists and there are subcategories each having a specific name, So for Artist gallery you want to display list of all the artists with their pictures.
I also needed to get list of child categories of selected parent category with url,
I found following code so useful
<?php
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 5
) );
foreach ( $categories as $category ) {
printf( '<li><a class="btn btn-light btn-light-custom mt-2" href="%1$s">%2$s</a></li>',
esc_url( get_category_link( $category->term_id ) ),
esc_html( $category->name )
);
}
?>
Replace "5" with your selected parent category ID

Resources