WP_Sitemaps_Taxonomies::get_url_list() WordPress Method

The WP_Sitemaps_Taxonomies::get_url_list() method is used to get a list of URLs for a given taxonomy. This is useful for creating a sitemap for a taxonomy, as it provides a way to get all of the URLs for a given taxonomy in one place. The WP_Sitemaps_Taxonomies::get_url_list() method takes two arguments: The first argument is the taxonomy for which you want to get the URLs. The second argument is an array of arguments. The only argument that is currently supported is 'hide_empty', which, if set to true, will cause empty terms to be excluded from the URL list. The WP_Sitemaps_Taxonomies::get_url_list() method returns an array of URLs for the given taxonomy.

WP_Sitemaps_Taxonomies::get_url_list( int $page_num, string $object_subtype = '' ) #

Gets a URL list for a taxonomy sitemap.


Parameters

$page_num

(int)(Required)Page of results.

$object_subtype

(string)(Optional) Taxonomy name.

Default value: ''


Top ↑

Return

(array[]) Array of URL information for a sitemap.


Top ↑

Source

File: wp-includes/sitemaps/providers/class-wp-sitemaps-taxonomies.php

	public function get_url_list( $page_num, $object_subtype = '' ) {
		// Restores the more descriptive, specific name for use within this method.
		$taxonomy        = $object_subtype;
		$supported_types = $this->get_object_subtypes();

		// Bail early if the queried taxonomy is not supported.
		if ( ! isset( $supported_types[ $taxonomy ] ) ) {
			return array();
		}

		/**
		 * Filters the taxonomies URL list before it is generated.
		 *
		 * Returning a non-null value will effectively short-circuit the generation,
		 * returning that value instead.
		 *
		 * @since 5.5.0
		 *
		 * @param array[]|null $url_list The URL list. Default null.
		 * @param string       $taxonomy Taxonomy name.
		 * @param int          $page_num Page of results.
		 */
		$url_list = apply_filters(
			'wp_sitemaps_taxonomies_pre_url_list',
			null,
			$taxonomy,
			$page_num
		);

		if ( null !== $url_list ) {
			return $url_list;
		}

		$url_list = array();

		// Offset by how many terms should be included in previous pages.
		$offset = ( $page_num - 1 ) * wp_sitemaps_get_max_urls( $this->object_type );

		$args           = $this->get_taxonomies_query_args( $taxonomy );
		$args['fields'] = 'all';
		$args['offset'] = $offset;

		$taxonomy_terms = new WP_Term_Query( $args );

		if ( ! empty( $taxonomy_terms->terms ) ) {
			foreach ( $taxonomy_terms->terms as $term ) {
				$term_link = get_term_link( $term, $taxonomy );

				if ( is_wp_error( $term_link ) ) {
					continue;
				}

				$sitemap_entry = array(
					'loc' => $term_link,
				);

				/**
				 * Filters the sitemap entry for an individual term.
				 *
				 * @since 5.5.0
				 * @since 6.0.0 Added `$term` argument containing the term object.
				 *
				 * @param array   $sitemap_entry Sitemap entry for the term.
				 * @param int     $term_id       Term ID.
				 * @param string  $taxonomy      Taxonomy name.
				 * @param WP_Term $term          Term object.
				 */
				$sitemap_entry = apply_filters( 'wp_sitemaps_taxonomies_entry', $sitemap_entry, $term->term_id, $taxonomy, $term );
				$url_list[]    = $sitemap_entry;
			}
		}

		return $url_list;
	}


Top ↑

Changelog

Changelog
VersionDescription
5.9.0Renamed $taxonomy to $object_subtype to match parent class for PHP 8 named parameter support.
5.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.