Home Forums General Issues Show products by custom fields


Show products by custom fields

  • Hi everyone,

    I hope someone can help. I’ve installed a Woocommerce shop. There are diffrent customer categories. Some categories could only be seen by customer category A for example. But in the products there’s an option to public the product although it’s in a “hidden” category.

    So I wrote a function in my functions.php but it didn’t work. Could someone help me? That would be very, very great! Thanks so far! 🙂

    add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
    function custom_pre_get_posts_query( $q ) {
    	$id_arr = array();
    	$id_count = 0; 
    	global $post;
    	$o = new WP_Query();
    	while ( $o->have_posts() ) : $o->the_post();
    		$terms = get_the_terms( $post->ID, 'product_cat' );
    		foreach ($terms as $term) {
    			$cat_id =  'term_' . $term->term_id;
    			if( (get_field('kundentyp', 'user_' . get_current_user_id()) < get_field('kundenzugang', $cat_id)) && (get_field('kundenzugang', $cat_id) != 'o') && (get_field('soll_das_produkt_offentlich_dargestellt_werden', $post->ID) == false)) {
    					$pro_id = $post->ID;
    					$id_arr[$id_count] = $pro_id;
    	//if ( ! $q->is_main_query() ) return;
    	//if ( ! $q->is_post_type_archive() ) return;
    	if ( ! is_admin() && is_shop() ) {
    		$q->set('post__not_in', $id_arr);
    		if ($q->is_search) {
    			$q->set('post__not_in', $id_arr);
    	remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
  • It really depends on what type of field you’re using. There are details about querying posts by different types of acf fields here

  • Thanks for your answer.

    But the field ‘kundentyp’ (type: radio button / transl.: typ of customer) is used in user profile and ‘kundenzugang’ (type: radio button / transl.: access of customer) is used in the productcategories.

    Only the field ‘soll_das_produkt_offentlich_dargestellt_werden’ (type: true/false / transl.: should the product be published?) is a field in the post.

    So the query-method won’t be the right one, or am I wrong?

  • You’re going to have to give a better explanation of what you’re trying to do because I don’t think I have enough information here to figure it out. Can you try again to explain how these things are connected and the conditions where a post should be show?

  • Okay I will try it.

    I’ve got a Woocommerce shop. There the customers can register. There are also four different customer categories: A, B, C and D. If a customers has registered, the admin can choose (with the radio button field ‘kundentyp’) on the profile page which typ the customer will be.

    Then there are the normal product categories. In the backend on the category page the admin also can choose (with a radio button field called ‘kundenzugang’) which customer category is allowed to visit this category.

    Every product has a field, where the admin can choose if the product should be published or not. So if there’s a customer B and want to visit the category ‘test’ where he hasn’t got an access (because only D is allowed to) and this category includes a product where this “public field” is tiggered, he is able to see this product in the frontend. Otherwise this public field isn’t tiggered he isn’t able to that product.

    So I’ve created a function for my functions.php which should go through all posts and check if the user is allowed to see the products. If there are product’s he isn’t able to see, the id of this product is written in an array ($id_arr). After that the array is taken to the ‘post__not_in’ to exclude them from the query.

    But my function (see first post) will not work and I don’t know why..

    I hope it’s a better explanation. Thanks for your help. 🙂

  • we changed the configuration of the product handling, so no more help needed – but thanks a lot so far. 🙂

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Show products by custom fields’ is closed to new replies.