Documentation

apply_filters("whereused_scan_meta", array $custom_meta_refs, string $meta_value, array $ref)

Details

  • whereused_scan_meta
  • filter hook
  • Active
  • 1.1.0

Parameters

$custom_meta_refs array
Array of WhereUsed/Reference objects
$meta_value string
Value of the post meta
$ref array
Array of values that must be passed to a new Reference object when adding a new Reference to the $custom_meta_refs array

Example Code: Create Reference – Associate Attachment ID in Custom Field to The Post

If you have a custom field that uses an ID, slug, or even a URL as a reference to a post, it will not show up in the references after a scan. You must use a custom hook so that WhereUsed knows how that information should be handled and tell WhereUsed the reference exists. The example below shows how to associate a post to the custom field with an attachment ID located in field name “product_online_file_id”.

Basically, we want WhereUsed to know that the attachment is being referenced by the post via the custom field.

Once the code has been implemented, you will need to rescan the posts that have these custom fields. You could either run a full scan, or go to the specific posts (if there are not many) and just resave them. The action of saving will initiate a new scan specifically on that post.

/**
 * Modify post meta scanning: Associate the attachment ID of a PDF
 *
 * @since 1.1.0
 *
 * @param array  $custom_meta_refs
 * @param array  $ref
 * @param string $meta_value
 *
 * @return array Must return an array of WhereUsed/Reference objects
 */
function custom_whereused_scan_meta( array $custom_meta_refs, string $meta_value, array $ref ): array {

	$where = $ref['from_where'] ?? '';

	if ( 'post meta' == $where ) {

		$meta_key = $ref['from_where_key'] ?? '';
		$post_type = $ref['from_post_type'] ?? '';

		if ( 'product' == $post_type && 'product_online_file_id' == $meta_key ) {

			// Let's make sure it's an integer
			$id = (int) $meta_value;

			if ( $id ) {
				// This is an attachment id of a PDF file

				$ref['to_post_id'] = $id; // post ID of the attachment
				$ref['to_post_type'] = 'attachment'; // Post type
				$ref['to_type'] = 'id'; // This attachment was referenced by an ID (link, image, iframe, block, id)

				/**
				 * Add this reference to the meta references array
				 * by creating a Reference object from the $args we have modified
				 * @link https://whereused.com/docs/classes/reference/
				 * @notice If a valid Reference object is not returned, then the entry added below will be skipped.
				 */
				$custom_meta_refs[] = new WhereUsed\Reference( $ref );
			}
		}
	}

	return $custom_meta_refs;
}
add_filter( 'whereused_scan_meta', 'custom_whereused_scan_meta', 10, 3 );

Source

This filter can be found in /where-used/inc/Scan.php

/**
 * Hook that allows you to handle the scan of a specific post meta
 *
 * @link  https://whereused.com/docs/hooks/whereused_scan_meta/
 * @since 1.1.0
 *
 * @param array  $custom_meta_refs
 * @param string $meta_value
 * @param array  $ref
 *
 * @return array Must return an array of WhereUsed/Reference objects
 */
$custom_meta_refs = apply_filters( 'whereused_scan_meta', $custom_meta_refs, $meta_value, $ref );

If you notice any incorrect or missing information on this page, please let us know.