Skip to content

Instantly share code, notes, and snippets.

@loorlab
Created January 28, 2019 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loorlab/51785ac1586582454e5c72b89422a177 to your computer and use it in GitHub Desktop.
Save loorlab/51785ac1586582454e5c72b89422a177 to your computer and use it in GitHub Desktop.
Get Yoast SEO Data WordPress via https://deanandrews.uk/get-yoast-seo-data/
<?php
/**
* Version: 4.0
* Date: 28/03/2018
* Source: https://deanandrews.uk/get-yoast-seo-data/
*
* Note:
* This code is provided "as is" and any expressed or implied warranties, including, but not limited to, the implied
* warranties of merchantability and fitness for a particular purpose are disclaimed.
* In no event shall the regents or contributors be liable for any direct, indirect, incidental, special, exemplary, or
* consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or
* profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or
* tort (including negligence or otherwise) arising in any way out of the use of this code, even if advised of
* the possibility of such damage.
*/
// Get the key data from the URL
function CheckAccess(){
return @$_GET['key']=='MySecretKEY';
}
// If the key is incorrect block access to data
if (!CheckAccess()){
header('HTTP/1.0 404 Not Found');
exit;
}
// The ouput filename
$out = fopen('Yoast Data Output.csv', 'w');
// Add column headers
fputcsv($out, array("Post ID","Post Type","URL","Focus Key Word","Page Title","Meta Description"));
require('../wp-load.php');
// Get the data
global $wpdb;
$posts = $wpdb->get_results("
SELECT (SELECT `meta_value` FROM `$wpdb->postmeta` WHERE `meta_key` = '_yoast_wpseo_focuskw' AND post_id = $wpdb->posts.ID) AS focuskw,
(SELECT `meta_value` FROM `$wpdb->postmeta` WHERE `meta_key` = '_yoast_wpseo_title' AND post_id = $wpdb->posts.ID) AS title,
(SELECT `meta_value` FROM `$wpdb->postmeta` WHERE `meta_key` = '_yoast_wpseo_metadesc' AND post_id = $wpdb->posts.ID) AS metadesc,
$wpdb->posts.ID, $wpdb->posts.post_type FROM `$wpdb->posts`
WHERE $wpdb->posts.post_status = 'publish'
AND (SELECT `meta_value` FROM `$wpdb->postmeta` WHERE `meta_key` = '_yoast_wpseo_meta-robots-noindex' AND post_id = $wpdb->posts.ID)is null
AND ($wpdb->posts.post_type = 'page' OR $wpdb->posts.post_type = 'post')
");
// Loop through the data and add to the csv output
foreach($posts as $post) {
$permalink = get_permalink($post->ID);
fputcsv($out, array(($post->ID),($post->post_type),($permalink),($post->focuskw),($post->title),($post->metadesc)));
}
// Output the csv file
fclose($out);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment