// --- Force Yoast to use Featured Image for og:image --- // 1) Remove Yoast per-post overrides so fallback = featured image. delete_post_meta($post_id, '_yoast_wpseo_opengraph-image'); delete_post_meta($post_id, '_yoast_wpseo_twitter-image'); // 2) Report featured image (if present). $featured_image_url = has_post_thumbnail($post_id) ? get_the_post_thumbnail_url($post_id, 'full') : null; // 3) Try multiple ways to (re)index the post in Yoast. $yoast_indexed = false; if ( function_exists('YoastSEO') ) { try { // (a) Helper path (some Yoast versions) if ( isset(YoastSEO()->helpers) && isset(YoastSEO()->helpers->indexable) ) { $h = YoastSEO()->helpers->indexable; if ( method_exists($h, 'for_post') && method_exists($h, 'index') ) { $indexable = $h->for_post($post_id); $h->index($indexable); $yoast_indexed = true; } elseif ( method_exists($h, 'index_post') ) { $h->index_post($post_id); $yoast_indexed = true; } elseif ( method_exists($h, 'build_for_post') ) { $h->build_for_post($post_id); $yoast_indexed = true; } } // (b) Action class path (newer Yoast exposes DI container) if ( ! $yoast_indexed && isset(YoastSEO()->classes) && method_exists(YoastSEO()->classes, 'get') ) { // Namespaced class exists in modern Yoast. if ( class_exists('\Yoast\WP\SEO\Actions\Indexing\Indexable_Post_Indexation_Action') ) { $action = YoastSEO()->classes->get(\Yoast\WP\SEO\Actions\Indexing\Indexable_Post_Indexation_Action::class); if ( $action && method_exists($action, 'index') ) { $action->index($post_id); $yoast_indexed = true; } } } } catch ( \Throwable $e ) { // swallow; we’ll try the fallback if ( defined('WP_DEBUG') && WP_DEBUG ) { error_log('[custom/trigger-update] Yoast index attempt error: ' . $e->getMessage()); } } } // (c) Last-resort: fake a save to trigger Yoast’s normal save_post hooks. if ( ! $yoast_indexed ) { // This updates post_modified and fires save hooks w/o changing content. wp_update_post( [ 'ID' => $post_id ] ); } // 4) Warm Yoast head endpoint. // NOTE: Use the raw permalink (no manual rawurlencode). add_query_arg will handle encoding. $yoast_head = null; $permalink = get_permalink($post_id); if ( $permalink ) { $yoast_head_url = add_query_arg( [ 'url' => $permalink ], rest_url('yoast/v1/get_head') ); $resp = wp_remote_get( $yoast_head_url, [ 'timeout' => 15, 'redirection' => 3 ] ); if ( is_wp_error($resp) ) { if ( defined('WP_DEBUG') && WP_DEBUG ) { error_log('[custom/trigger-update] Yoast head request error: ' . $resp->get_error_message()); } } else { $code = wp_remote_retrieve_response_code($resp); if ( $code === 200 ) { $yoast_head = json_decode( wp_remote_retrieve_body($resp), true ); } else { if ( defined('WP_DEBUG') && WP_DEBUG ) { error_log('[custom/trigger-update] Yoast head HTTP ' . $code . ' URL=' . $yoast_head_url); } } } } // Optional: clear WP object cache entry for this post. clean_post_cache($post_id);