Display Posts in Looper which are from Custom Post Type or Custom Taxonomy

I’ve got a CPT called Business Services and a custom taxonomy called Business Services. I want to display posts in my looper that are from the Business Services CPT or have been tagged with the Business Services taxonomy. I’ve tried creating multiple arrays for a var dump, but nothing seems to work.

This is my array

$query = array(
  'post_type'           => 'business_ser_dir_ltg',
  'post_status'         => 'publish',
  'ignore_sticky_posts' => 1,
  'posts_per_page'      => -1,
  'orderby'             => '{{dc:url:param key="orderby" fallback="rand"}}',
  'order'               => '{{dc:url:param key="order" fallback="dsc"}}',
  'offset'              => 0,
  'category_name'       => '{{dc:term:slug}}',
  'tax_query'           => array(
          'relation' => 'AND',
          array(
                'taxonomy' => 'secondary_listing_category',
                'field'    => 'slug',
                'terms'    => 'business-services',
                'operator' => 'IN',
          ),
      ),
  );

var_dump( http_build_query( $query ) );

This is query that was built, but does not work:
post_type=business_ser_dir_ltg&post_status=publish&ignore_sticky_posts=1&posts_per_page=-1&orderby={{dc:url:param key="orderby" fallback="rand"}}&order={{dc:url:param key="order" fallback="dsc"}}&offset=0&category_name={{dc:term:slug}}&tax_query%5Brelation%5D=AND&tax_query%5B0%5D%5Btaxonomy%5D=secondary_listing_category&tax_query%5B0%5D%5Bfield%5D=slug&tax_query%5B0%5D%5Bterms%5D=business-services&tax_query%5B0%5D%5Boperator%5D=IN

What do I need to change in my array to get the looper to display posts from my CPT and custom taxonomy?

Hey @OctoCog,

Thanks for writing in! Please explore by removing 'category_name' => '{{dc:term:slug}}', in your query argument and generate a new query string. If it does not help, we would love to inspect your looper. You can create a secure note in your next reply with the following info:
– Link to your site
– WP login URL
– WP username
– WP password
– WP Administrator Role
– Confirmation that we can access and make changes to your site

To know how to create a secure note, please check this out: How The Forum Works

image

Best Regards.

Hi @ruenel

Thank you for the recommendation, but it didn’t work. I tagged The Lobster Shanty under Eat, Drink, Stay the custom post type and tagged it ‘Business Services’.

I added a secure note. You can go ahead and access the site and make whatever changes are necessary.

Hey @OctoCog,

I have checked your custom post type. I am just confused because none of the Business Services listings has the business-services taxonomy assigned to it. You can either add the post types in an array

'post_type'  => array('business_ser_dir_ltg', 'eat_drink_dir_ltg', 'non_profits_dir_ltg', 'retail_dir_ltg', 'the_arts_dir_ltg'),

or completely change your query argument into just this one:

$query = array(
  'post_type'           => 'any',
  'post_status'         => 'publish',
  'ignore_sticky_posts' => 1,
  'posts_per_page'      => -1,
  'orderby'             => 'rand',
  'order'               => 'desc',
  'offset'              => 0,
  'tax_query'           => array(
          array(
                'taxonomy' => 'secondary-listing-category',
                'field'    => 'slug',
                'terms'    => 'business-services',
                'operator' => 'IN',
          ),
      ),
  );

This means that the looper will return any items that have the “Business Taxonomy” taxonomy.

Best Regards.

Hey @ruenel ,

Maybe a small explanation would help. My client wanted a way to put a listing under two different post types (Example: Business Services and Eat, Drink, Stay). Seeing as it’s not possible to have a listing under two custom post types, I created a workaround. The workaround was the Secondary Listing Category custom taxonomy.

These are the Listing custom post types:
Business Services
Eat, Drink, Stay
Non-Profits
Retail
The Arts
Tourism
Wellness

These are the Secondary Listing Categories (mirroring the same as above):
Business Services
Eat, Drink, Stay
Non-Profits
Retail
The Arts
Tourism
Wellness

This would give my client the ability to put the listing “The Lobster Shanty” under a custom post type like Eat, Drink, Stay and then tag it the custom taxonomy “Business Services”. This will make it so the “The Lobster Shanty” listing can appear on the Eat, Drink, Stay directory page AND the Business Services directory page. It’s a pseudo way of listing one post under two directories.

All I’m trying to do now is have both the CPT Business Services posts and posts tagged with the custom taxonomy Business Services appear on the same directory page.

Hello @OctoCog,

If that is the case then the argument would be:

$args = array(
    'post_type' 		  => array('business_ser_dir_ltg', 'any'), // Include the Business Services CPT and any post type
    'post_status'         => 'publish',
	'ignore_sticky_posts' => 1,
	'posts_per_page'      => -1,
	'orderby'             => 'rand',
	'order'               => 'desc',
	'offset'              => 0,
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'secondary-listing-category',
            'field'    => 'slug',
            'terms'    => 'business-services',
        ),
    ),
);

Kindly let us know how it goes.

@ruenel Thank you! We are getting closer but not quite there yet. I’ve added the WPQuery string to the provider and now it is just outputting the single post tagged with the Secondary Listing Category “Business Services”. All the other “business_ser_dir_ltg” posts are missing. They should be there as well.

Hey @OctoCog,

Take this query for example:

$args = array(
    'post_type'       => array('business_ser_dir_ltg', 'eat_drink_dir_ltg', 'non_profits_dir_ltg', 'retail_dir_ltg', 'the_arts_dir_ltg', 'tourism_dir_ltg', 'wellness_dir_ltg'), // Include the Business Services CPT and any post type
    'post_status'         => 'publish',
  'ignore_sticky_posts' => 1,
  'posts_per_page'      => -1,
  'orderby'             => 'rand',
  'order'               => 'desc',
  'offset'              => 0,
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'secondary-listing-category',
            'field'    => 'slug',
            'terms'    => 'business-services',
        ),
    ),
);

Because we added a taxonomy query, we limit the post items to return only those that are under the specific taxonomy. You cannot display “ALL” business services custom post types and any other post type under the business services taxonomy. You may need to use custom PHP code for the Looper Provider Custom that will have to query them separately and then merge them into a single object array.

Hope this makes sense.

@ruenel Okay, I’ll try to put together custom PHP in order to achieve this. I’ll report back if I find a solution.

Hey @OctoCog,

Great and let us know how it goes.

Thank you.

@ruenel Thank you for your help :slightly_smiling_face:

I created a solution with custom PHP code. I used a Custom Looper Provider and used the code below to combine all posts of a CPT and CPT posts with a custom taxonomy.

add_filter( 'cs_looper_custom_business_services_data', function( $result, $merged_posts) {
$args_a = get_posts(  
        array(
            'post_type'           => 'business_ser_dir_ltg', 
            'post_status'         => 'publish',
            'ignore_sticky_posts' => 1,
            'posts_per_page'      => -1,
            'offset'              => 0,
            'offset'              => 0,
        ));



$args_b = get_posts(
        array(
            'post_type'       => array('business_ser_dir_ltg', 'eat_drink_dir_ltg', 'non_profits_dir_ltg', 'retail_dir_ltg', 'the_arts_dir_ltg', 'tourism_dir_ltg', 'wellness_dir_ltg'), // Include the Business Services CPT and any post type
            'post_status'         => 'publish',
            'ignore_sticky_posts' => 1,
            'posts_per_page'      => -1,
            'offset'              => 0,
                'tax_query' => array(
                    'relation' => 'OR',
                    array(
                        'taxonomy' => 'secondary-listing-category',
                        'field'    => 'slug',
                        'terms'    => 'business-services',
                    ),
        ),
    ),
);
$merged_posts = array_merge($args_a, $args_b); //Merge Business Services CPT with listed CPTs that have the taxonomy business-services
shuffle ($merged_posts); //Randomize all the posts
return $merged_posts; //Return those Randomized Posts

}, 10, 3);
1 Like

You are most welcome and thanks for sharing the information.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.