Created
April 13, 2022 17:36
-
-
Save brandonkramer/340351b94b761d23bcb0543adcbee2a3 to your computer and use it in GitHub Desktop.
Get the number of internal links linking to a post and the number of outgoing internal links from Yoast indexable using their models.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( !class_exists( '\Yoast\WP\Lib\Model' ) || !class_exists( '\Yoast\WP\SEO\Models\Indexable' ) ) { | |
return; | |
} | |
// The post ID | |
$object_id = 123; | |
// The post object type (post type) | |
$object_type = 'post'; | |
/* @var $indexable \Yoast\WP\SEO\Models\Indexable */ | |
$indexable = \Yoast\WP\Lib\Model::of_type( 'Indexable' )->where( 'object_id', $object_id ) | |
->where( 'object_type', $object_type ) | |
->find_one(); | |
// Number of internal links linking to the post ID | |
$internal_links = (int) $indexable->incoming_link_count; | |
// Number of outgoing internal links to the post ID | |
$outgoing_links = (int) $indexable->link_count; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment