Share attributes / taxonomies with other post types

Is there a way to have a custom taxonomy included in the listing attributes (and therefore the submit listing form), or to make an attribute accessible for other post types, e.g. blog posts?

I am creating a website that has a listing section for buy & sell via Hivepress, but also a Review section. Both the reviews (WP posts, not listings) and the Buy & Sell (hp Listings) share some of the same metadata and I would like to use some common taxonomies to keep ensure that the options remain in sync.

Thanks

I have managed to use the code below to populate an attribute on listings with the values from my custom taxonomy which is shared with my other posts & custom post types.

I am not sure if this is the best way to do this, but it looks promising.

function get_manufacturers_slugs_and_names() {
    // Get all terms from the 'manufacturers' taxonomy
    $terms = get_terms([
        'taxonomy'   => 'manufacturer',
        'hide_empty' => false,
    ]);

    // Initialize an empty array
    $manufacturers_array = array();

    // Loop through each term and add its slug and name to the array
    if (!empty($terms) && !is_wp_error($terms)) {
        foreach ($terms as $term) {
            $manufacturers_array[$term->slug] = $term->name;
        }
    }
    
    return $manufacturers_array;
}

add_filter(
	'hivepress/v1/models/listing/attributes',
	function ($attributes) {
		// Get the slugs and names from the 'manufacturers' taxonomy
		$manufacturers = get_manufacturers_slugs_and_names();

		// Add the manufacturers to the field options
		$attributes['Manufacturers'] = [
			'editable'      => true,
			'filterable'    => true,

			'display_areas' => [
				'view_page_secondary',
			],

			'edit_field'    => [
				'label'     => 'Manufacturer',
				'type'      => 'select',
				'options' 	=> array_merge($manufacturers),
				'_external' => true,
				'_order'    => 30,
			],

			'search_field'    => [
				'label'     => 'Manufacturer',
				'type'      => 'select',
				'options' 	=> array_merge($manufacturers),
				'_external' => true,
				'_order'    => 30,
			],
		];

		return $attributes;
	},
	1000
);

Hi,

Yes, the posted solution seems to be ok if you just want to sync the actual options and not share the taxonomy itself including all the related features. If you add blog post support for listing taxonomies then category/attribute pages may show mixed results (listings and posts) and there may be other related issues.

Thanks @ihor, what would be the approach to actually share the taxonomy? If I add blog post support for hp_listing is there a way to use this like an attribute, adding it to the details form and displaying it in the page/block sections?

Sorry for the delay.

You can try this function to share an existing taxonomy register_taxonomy_for_object_type() ā€“ Function | Developer.WordPress.org The taxonomy name for a custom attribute is ā€œhp_listing_attributenamehereā€, the object type is ā€œpostā€. Please make sure that the code fires after the attribute taxonomies are registered, you can try different WordPress hooks for this (any hook that fires after ā€œinitā€) Action Reference ā€“ Common APIs Handbook | Developer.WordPress.org

Please note that if you have Public option enabled for these attributes there may be issues, e.g. attribute pages may attempt to display posts mixed with listings.

Sorry, canā€™t fully understand the required functionality related to posts, but if you mean selecting them inside the listing form (e.g. using posts as taxonomy options), this would require further customizations.

Hope this helps

1 Like

Thanks @ihor, sorry I used very poor terminology in my last post. What I meant to say was if I register a custom taxonomy to the hp_listing object, is there a way to then include this custom taxonomy value in the listing display areas, like I can for an attribute?

I have been unable to do the reverse, and register an attribute to another object (ā€˜postā€™) using the code below. I tried to register ā€˜hp_listing_makeā€™ and a custom tax (created via a plugin) called ā€˜manufacturerā€™. Only the ā€˜manufacturerā€™ taxonomy is available for the the ā€˜postā€™ object, and the ā€˜hp_listing_makeā€™ attribute does not appear as available for posts. Any suggestions?

Thanks,
Brendan

function hivepress_attach_attribute_to_post() {
    register_taxonomy_for_object_type('hp_listing_make', 'post');
}

add_action('wp_loaded', 'hivepress_attach_attribute_to_post');


function hivepress_attach_attribute_to_post_2() {
    register_taxonomy_for_object_type('manufacturer', 'post');
}

add_action('wp_loaded', 'hivepress_attach_attribute_to_post_2');

Ah, I had not made the hp_listing_attribute public. Got it now, thanks.

Just for reference, this is the code I am using to add the attribute ā€˜conditionā€™ to post types ā€˜postā€™ and ā€˜reviewā€™.

// Make attribute public

function hivepress_make_hp_listing_condition_public() {
    // get the arguments of the already-registered taxonomy
    $hp_listing_condition_args = get_taxonomy( 'hp_listing_condition' ); // returns an object

    // make changes to the args
	$hp_listing_condition_args->public = true;
	$hp_listing_condition_args->show_in_quick_edit  = true;
	$hp_listing_condition_args->show_in_rest = true;
    $hp_listing_condition_args->rewrite['slug'] = 'condition';
    $hp_listing_condition_args->rewrite['with_front'] = false;

    // re-register the taxonomy and add additional post types
    register_taxonomy( 'hp_listing_condition', array('listing','post','review'), (array) $hp_listing_condition_args );

}
// Run action after init hook to ensure attribute already exists
add_action( 'wp_loaded', 'hivepress_make_hp_listing_condition_public');

Sorry for the delay.

Yes, itā€™s possible, but I recommend registering the taxonomy via the HivePress UI. This happens automatically if you add a selectable (Select, Checkboxes or Radio Buttons) attribute, then thereā€™s a taxonomy with ā€œhp_listing_attributenamehereā€ name. Another approach (if you need to register it via the code) is to register a taxonomy that matches the attribute taxonomy name, this way you can control where itā€™s displayed via the UI (by selecting Display Areas), while also customizing the taxonomy configuration via the code.

1 Like

Thanks again @ihor. The fact that only selectable attributes are registered as a taxonomy is interesting. I found that if I create a text attribute then register it to other post types the values created on listings are not shared with the other post types. This explains why.

1 Like

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