Posted on

Download Monitor: Index PDF content for parent page

Relevanssi can handle Download Monitor files without big problems. If you want to use the “Index PDF contents for the parent page” option, there’s a problem, though. Download Monitor comes in between: the PDF post is attached to the Download Monitor dlm_download post and not the page where the [download] shortcode is.

This can be solved. It’s easiest if the shortcode is added to a custom field, in which case we can just pick up the custom field, get the download post ID from that and then use that in the query to get the attachment content.

Add this to your theme functions.php and replace download in the get_post_meta() with the name of the custom field that has the [download] short code:

add_filter( 'relevanssi_pdf_for_parent_query', 'rlv_download_monitor', 10, 2 );

/**
 *  Modifies the attachment content query.
 *
 * @param string $query   The MySQL query for fetching the attachment content.
 * @param int    $post_id The post ID of the post where the PDF content is added.
 *
 * @return string The modified MySQL query.
 */
function rlv_download_monitor( string $query, int $post_id ) : string {
    $meta_field_value = get_post_meta( $post_id, 'download', true );
    if ( $meta_field_value ) {
        // The [download id="1234"] shortcode format has one digit in it, so let's grab that.
        $download_id = preg_replace( '/[^\d]/', '', $meta_field_value );
        if ( $download_id ) {
            // Replace the original post_parent ID with the new one.
            $query = preg_replace( '/post_parent = \d+/', "post_parent = $download_id", $query );
        }
    }

    return $query;
}

If the [download] shortcode is somewhere in the post content, the method is slightly different:

add_filter( 'relevanssi_pdf_for_parent_query', 'rlv_download_monitor', 10, 2 );

/**
 *  Modifies the attachment content query.
 *
 * @param string $query   The MySQL query for fetching the attachment content.
 * @param int    $post_id The post ID of the post where the PDF content is added.
 *
 * @return string The modified MySQL query.
 */
function rlv_download_monitor( $query, $post_id ) {
    $post_object = relevanssi_get_post( $post_id );
    $id_matches  = preg_match( '/\[download id=.(\d+).\]/ims', $post_object->post_content, $matches );
    if ( $id_matches ) {
        $download_id = $matches[1];
        $query       = preg_replace( '/post_parent = \d+/', "post_parent = $download_id", $query );
    }

    return $query;
}

Here the code looks for [download id="1234"] shortcode somewhere in post_content and picks up the digits in it. This works if there’s just one shortcode in the post; this takes the first match. If there are multiple matches, you need use preg_match_all() and change the post_parent to IN(1, 2, 3) matching.

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.