View Categories

Related Recipes

Default Attributes #

‘cooked_related_recipes_default_atts’
This filter lets you customize the default attributes for the [cooked-related-recipes] shortcode. Use it to modify default weights, limits, matching options, or any other shortcode attribute defaults.

Parameters #

  • $default_atts (array): The default shortcode attributes array.

Return (Required) #

You must return an array of default attributes matching the structure of the original array.

Example #

add_filter( 'cooked_related_recipes_default_atts', function( $default_atts ) {
    // Increase ingredient weight for better ingredient-based matching
    $default_atts['ingredient_weight'] = 12;

    // Change default limit to 8 recipes
    $default_atts['limit'] = 8;

    // Disable difficulty matching by default
    $default_atts['match_difficulty'] = false;

    return $default_atts;
} );

Individual Recipe Score #

‘cooked_related_recipes_recipe_score’
This filter lets you modify or exclude individual recipe scores during the calculation process. Use it to adjust scores based on custom logic, exclude specific recipes, or add custom scoring factors.

Parameters #

  • $recipe_score (array): The recipe score data ['id' => int, 'score' => int].
  • $rid (int): The recipe ID being scored.
  • $source_recipe (array): The source recipe data array.
  • $atts (array): The shortcode attributes.

Return (Required) #

You must return an array with ['id' => int, 'score' => int] or false/null to exclude the recipe. If score is 0 or negative, the recipe will be excluded.

Example #

add_filter( 'cooked_related_recipes_recipe_score', function( $recipe_score, $rid, $source_recipe, $atts ) {
    // Boost score for recipes from the same author
    if ( isset( $source_recipe['author']['id'] ) ) {
        $recipe_post = get_post( $rid );
        if ( $recipe_post && (int) $recipe_post->post_author === (int) $source_recipe['author']['id'] ) {
            $recipe_score['score'] += 5; // Add bonus points
        }
    }

    // Exclude recipes with specific meta
    if ( get_post_meta( $rid, '_exclude_from_related', true ) === 'yes' ) {
        return false; // Exclude this recipe
    }

    return $recipe_score;
}, 10, 4 );

Scores Before Sorting #

‘cooked_related_recipes_scores_before_sort’
This filter lets you modify all recipe scores before they are sorted. Use it for bulk modifications, additional filtering, or score adjustments that need to happen before sorting.

Parameters #

  • $scores (array): Array of recipe scores [ ['id' => int, 'score' => int], ... ].
  • $source_recipe (array): The source recipe data array.
  • $atts (array): The shortcode attributes.

Return (Required) #

You must return an array of recipe scores in the same format.

Example #

add_filter( 'cooked_related_recipes_scores_before_sort', function( $scores, $source_recipe, $atts ) {
    // Remove recipes with scores below a threshold
    $scores = array_filter( $scores, function( $item ) {
        return $item['score'] >= 10; // Only keep recipes with score >= 10
    } );

    // Normalize scores to 0-100 range
    if ( ! empty( $scores ) ) {
        $max_score = max( array_column( $scores, 'score' ) );
        if ( $max_score > 0 ) {
            foreach ( $scores as &$score_item ) {
                $score_item['score'] = round( ( $score_item['score'] / $max_score ) * 100 );
            }
        }
    }

    return $scores;
}, 10, 3 );

Final Sorted Scores #

‘cooked_related_recipes_scores’
This filter lets you modify the final sorted scores after sorting has been applied. Use it for final modifications, reordering, or filtering after the default sort.

Parameters #

  • $scores (array): Array of sorted recipe scores [ ['id' => int, 'score' => int], ... ].
  • $source_recipe (array): The source recipe data array.
  • $atts (array): The shortcode attributes.

Return (Required) #

You must return an array of recipe scores in the same format.

Example #

add_filter( 'cooked_related_recipes_scores', function( $scores, $source_recipe, $atts ) {
    // Limit to top 5 recipes regardless of shortcode limit
    $scores = array_slice( $scores, 0, 5 );

    // Add featured recipes to the top
    $featured_ids = get_posts( [
        'post_type' => 'cp_recipe',
        'meta_key' => '_featured_recipe',
        'meta_value' => 'yes',
        'fields' => 'ids',
        'posts_per_page' => 2,
    ] );

    foreach ( $featured_ids as $featured_id ) {
        // Check if already in results
        $found = false;
        foreach ( $scores as $score_item ) {
            if ( $score_item['id'] === $featured_id ) {
                $found = true;
                break;
            }
        }
        if ( ! $found ) {
            array_unshift( $scores, [ 'id' => $featured_id, 'score' => 999 ] );
        }
    }

    return $scores;
}, 10, 3 );

Display Recipe IDs #

‘cooked_related_recipes_display_ids’
This filter lets you modify which recipe IDs are displayed before the HTML is rendered. Use it to add, remove, or reorder recipes in the final display.

Parameters #

  • $recipe_ids (array): Array of recipe IDs to display.
  • $related_recipes (array): Full related recipes array with scores [ ['id' => int, 'score' => int], ... ].
  • $recipe_id (int): The source recipe ID.
  • $atts (array): The shortcode attributes.

Return (Required) #

You must return an array of recipe IDs.

Example #

add_filter( 'cooked_related_recipes_display_ids', function( $recipe_ids, $related_recipes, $recipe_id, $atts ) {
    // Always include a specific "related" recipe if it exists
    $must_include_id = 123; // Your recipe ID
    if ( ! in_array( $must_include_id, $recipe_ids ) ) {
        array_unshift( $recipe_ids, $must_include_id );
    }

    // Remove recipes from a specific category
    $exclude_category = 'desserts';
    $recipe_ids = array_filter( $recipe_ids, function( $rid ) use ( $exclude_category ) {
        $terms = wp_get_object_terms( $rid, 'cp_recipe_category', [ 'fields' => 'slugs' ] );
        return ! in_array( $exclude_category, $terms );
    } );

    return $recipe_ids;
}, 10, 4 );

Output HTML #

‘cooked_related_recipes_output’
This filter lets you customize or completely replace the HTML output of the related recipes shortcode. Use it to change the markup, add custom elements, or replace the entire output.

Parameters #

  • $output (string): The shortcode HTML output.
  • $recipe_ids (array): Array of recipe IDs being displayed.
  • $recipe_id (int): The source recipe ID.
  • $atts (array): The shortcode attributes.

Return (Required) #

You must return a string containing the HTML output.

Example #

add_filter( 'cooked_related_recipes_output', function( $output, $recipe_ids, $recipe_id, $atts ) {
    // Wrap output in custom container
    $custom_output = '<div class="my-custom-related-wrapper">';
    $custom_output .= '<p class="related-intro">Check out these related recipes:</p>';
    $custom_output .= $output;
    $custom_output .= '</div>';

    return $custom_output;
}, 10, 4 );

Cache TTL #

‘cooked_related_recipes_cache_ttl’
This filter lets you customize how long related recipes are cached. The default is 7 days.

Parameters #

  • $ttl (int): Cache time-to-live in seconds (default: 604800 = 7 days).

Return (Required) #

You must return an integer representing seconds.

Example #

add_filter( 'cooked_related_recipes_cache_ttl', function( $ttl ) {
    // Cache for 14 days instead of 7
    return 14 * DAY_IN_SECONDS;
} );

Batch Size #

‘cooked_related_recipes_batch_size’
This filter lets you customize how many recipes are processed per batch during calculation. Lower values use less memory but take longer. Higher values are faster but use more memory.

Parameters #

  • $batch_size (int): Number of recipes to process per batch (default: 100).

Return (Required) #

You must return an integer.

Example #

add_filter( 'cooked_related_recipes_batch_size', function( $batch_size ) {
    // Process 50 recipes at a time for lower memory usage
    return 50;
} );

Query Arguments #

‘cooked_related_recipes_query_args’
This filter lets you modify the WP_Query arguments used to fetch recipes for comparison. Use it to add custom query parameters, tax queries, meta queries, or other WP_Query modifications.

Parameters #

  • $query_args (array): WP_Query arguments array.
  • $current_language (string|false): Current language code if multilingual is active, false otherwise.

Return (Required) #

You must return an array of WP_Query arguments.

Example #

add_filter( 'cooked_related_recipes_query_args', function( $query_args, $current_language ) {
    // Only include recipes published in the last year
    $query_args['date_query'] = [
        [
            'after' => '1 year ago',
        ],
    ];

    // Exclude specific recipe IDs
    $exclude_ids = [ 100, 200, 300 ];
    if ( isset( $query_args['post__not_in'] ) ) {
        $query_args['post__not_in'] = array_merge( $query_args['post__not_in'], $exclude_ids );
    } else {
        $query_args['post__not_in'] = $exclude_ids;
    }

    return $query_args;
}, 10, 2 );

Usage Notes #

  • All filters should be added in your theme’s functions.php or a custom plugin.
  • Filters are executed in the order they are registered (priority matters).
  • Return values must match the expected format or the filter may be ignored.
  • For performance, avoid heavy operations in filters that run for every recipe during calculation.
  • Use the cooked_related_recipes_scores_before_sort filter for bulk operations before sorting.
  • Use the cooked_related_recipes_scores filter for final modifications after sorting.