-
I have copied the code exactly from your example “return the number of posts in the chosen taxonomy term when used in the taxonomy pages” here: https://rankmath.com/kb/variables-in-seo-title-description/
It works well most of the time except when an archive is accessed from a different CPT created using Advanced Custom Forms.
PHP Warning: Undefined property: WP_Post_Type::$count in XXXXX/XXXXXX/functions.php
The line that errors is: return isset($term) ? $term->count : null;The whole code I have is:
/**
* Action: 'rank_math/vars/register_extra_replacements' - Allows adding extra variables.
* Snippet to register variable that will return the number of posts in the current term
*/
add_action('rank_math/vars/register_extra_replacements', function () {
rank_math_register_var_replacement(
'term_count',
[
'name' => esc_html__('Term Count', 'rank-math'),
'description' => esc_html__('Number of posts in the current term', 'rank-math'),
'variable' => 'term_count',
'example' => term_count_callback(),
],
'term_count_callback'
);
});
function term_count_callback()
{
$term = get_queried_object();
if(!empty($term)){
return isset($term) ? $term->count : null;
}
}
The ticket ‘Error when adding custom variable’ is closed to new replies.