Default category sorting for requests, vendors, and listings

Hello,

On my website, I’m using the taxonomies for Listings, Vendors, and Requests, and I want the categories to be the same across all three. I created a snippet to duplicate the categories from Listings into Vendors and Requests, and it works well in terms of replication.

However, I’ve encountered an issue: the categories in Listings are ordered correctly by the order field, while the categories in Vendors and Requests are sorted alphabetically (A to Z). This creates an inconsistency in the display.

Is there a native way to apply the order field sorting to Vendors and Requests as well? If not, could you point me to the files or sections of the theme/plugin where the category ordering logic is handled, so I can attempt to modify it manually?

Any guidance or hints would be greatly appreciated.

Thank you in advance for your help.

Hello,

Would you share your code?

I am, for one, interested.

function force_import_listing_categories_to_requests() {
    // Obtener todas las categorías de Listings
    $terms = get_terms([
        'taxonomy'   => 'hp_listing_category',
        'hide_empty' => false, // Incluir categorías vacías
    ]);

    // Verificar que existan categorías
    if (!empty($terms) && !is_wp_error($terms)) {
        // Crear un mapa para rastrear las relaciones entre términos
        $term_map = [];

        foreach ($terms as $term) {
            // Verificar si ya existe en Requests para evitar duplicados
            $existing_term = get_term_by('slug', $term->slug, 'hp_request_category');
            if ($existing_term) {
                error_log('Categoría ya existe: ' . $term->name);
                $term_map[$term->term_id] = ['term_id' => $existing_term->term_id];
                continue; // Saltar a la siguiente categoría
            }

            // Inicializar el ID del término principal
            $parent_id = 0;

            // Si la categoría tiene una principal, buscarla en la taxonomía de destino
            if ($term->parent) {
                if (isset($term_map[$term->parent])) {
                    $parent_id = $term_map[$term->parent]['term_id']; // Usar el ID mapeado
                } else {
                    // Buscar en la taxonomía de destino si no está mapeada
                    $parent_term = get_term_by('id', $term->parent, 'hp_request_category');
                    $parent_id = $parent_term ? $parent_term->term_id : 0;
                }
            }

            // Crear la categoría en la taxonomía de destino
            $new_term = wp_insert_term(
                $term->name, // Nombre de la categoría
                'hp_request_category', // Taxonomía de destino
                [
                    'slug'   => $term->slug, // Usar el mismo slug
                    'parent' => $parent_id, // Asignar el ID del término principal
                ]
            );

            // Registrar la categoría en el mapa si se creó correctamente
            if (!is_wp_error($new_term)) {
                $term_map[$term->term_id] = $new_term;
                error_log('Categoría importada: ' . $term->name);
            } else {
                error_log('Error al importar categoría: ' . $term->name . ' - ' . $new_term->get_error_message());
            }
        }
    } else {
        error_log('No se encontraron categorías en hp_listing_category.');
    }
}
add_action('init', 'force_import_listing_categories_to_requests');

1 Like

This is only for listings and requests, for now. But for Vendors it should be the same

1 Like

Gracias Manuel !
Muy amable.

1 Like

Hi,

Please clarify where exactly this order is used and we will try to help. Since there is no Categories block in requests and vendors now.

Hello,

Thank you for your response. I would like to clarify that I can already see the “Categories” section in Listings, Vendors, and Requests.

However, the issue is with how the categories are displayed:

  • In Listings, the categories are ordered using an “Order” field, which allows me to define a custom hierarchy and display the categories (including parent and subcategories) in the desired order.
  • In Vendors and Requests, the categories are displayed alphabetically (A-Z) instead of following the same “Order” field as in Listings.

What I want is for the categories in Vendors and Requests to use the same “Order” field logic as in Listings, so the display is consistent across all three sections.

Hi,

Please use these PHP snippets:

  1. For vendor categories:
add_filter(
	'hivepress/v1/meta_boxes',
	function( $meta_boxes ) {
		$meta_boxes['vendor_category_settings'] = [
			'screen' => 'vendor_category',

			'fields' => [
				'sort_order' => [
					'label'     => 'Order',
					'type'      => 'number',
					'min_value' => 0,
					'default'   => 0,
					'required'  => true,
					'_order'    => 20,
				],
			],
		];

		return $meta_boxes;
	}
);

For request categories:

add_filter(
	'hivepress/v1/meta_boxes',
	function( $meta_boxes ) {
		$meta_boxes['request_category_settings'] = [
			'screen' => 'request_category',

			'fields' => [
				'sort_order' => [
					'label'     => 'Order',
					'type'      => 'number',
					'min_value' => 0,
					'default'   => 0,
					'required'  => true,
					'_order'    => 20,
				],
			],
		];

		return $meta_boxes;
	}
);

​I hope this is helpful to you.

1 Like

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