Get Yoast SEO Data

By Dean Andrews | Posted on October 30, 2012

I love the On-page Optimisation in MOZ. This feature allows you to enter your focus keyword against a page for the url you wish to score.
Personally I add my focus keywords with a label just to keep it tidy when reporting back. However you can not extract the url from the database as it is created via the get_permalink() or get_page_link() functions.
I created GetYoastData which outputs to the browser the required data that can be saved into an csv (Excel) file.

Grab the code below and save it to a file called GetYoastData.php
Upload via FTP to the root directory (where the wp-config.php file is)
Visit page www.YOUR-DOMAIN.COM/GetYoastData.php
Select save page as GetYoastData.txt (Yes save as .txt)
Open excel and open your GetYoastData.txt file
Delete GetYoastData.php via FTP as I wouldn’t leave it hanging around for your competitors to find

<?php

/*
Version: 2.0
Date: 13/05/2014
Applied changes re get_permalink() as suggested by Joost de Valk
Compatible with WordPress SEO - Version 1.5.3 



Version: 1.0
Date: 29/10/2012
Source: http://deanandrews.uk/get-yoast-seo-data/

Upload via FTP to the root directory (where the wp-config.php file is)
Visit page www.YOUR-DOMAIN.COM/GetYoastData.php
Select save page as GetYoastData.txt (Yes save as .txt)
Open excel and open your GetYoastData.txt file
Delete GetYoastData.php via FTP as I wouldn't leave it hanging around for your competitors to find

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.
*/

echo "Post ID\tPost Type\tPermalink\tFocus Key Word\tPage Title\tMeta Description"; 
include "wp-load.php";

global $wpdb;
$posts = $wpdb->get_results("
SELECT (SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key` = '_yoast_wpseo_focuskw' AND post_id = wp_posts.ID)  AS focuskw,
(SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key` = '_yoast_wpseo_title' AND post_id = wp_posts.ID)  AS title,
(SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key` = '_yoast_wpseo_metadesc' AND post_id = wp_posts.ID)  AS metadesc,
wp_posts.ID, wp_posts.post_type FROM `wp_posts`
WHERE wp_posts.post_status = 'publish'
AND (SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key` = '_yoast_wpseo_meta-robots-noindex' AND post_id = wp_posts.ID)is null
AND (wp_posts.post_type = 'page' OR wp_posts.post_type = 'post') 
");

header('Content-type:text/plain');
foreach($posts as $post) {
	$permalink = get_permalink($post->ID);
	echo "\n{$post->ID}\t{$post->post_type}\t{$permalink}\t{$post->focuskw}\t{$post->title}\t{$post->metadesc}";
}

13 Replies to "Get Yoast SEO Data"

  • Joe Connor
    May 9, 2014 (11:10 pm)
    Reply

    Hi, this is exactly what I need but unfortunately when added to the root directory of my site the yoast data is not retrieved – just the headers and a blank page are displayed.
    I don’t have a config.php anywhere within my WP setup – there’s the usul wp-config.php in the root. Any idea why it doesn’t work?

    • Dean Andrews
      May 12, 2014 (12:18 pm)
      Reply

      Hi Joe,
      I suspect if you cannot see the wp-config.php then you may be in the wrong file path. The config file is required for WordPress to work.

  • Joe Connor
    May 13, 2014 (1:47 am)
    Reply

    Hi Dean, I have uploaded GetYoastData.php to the same location as wp-config.php (your instructions say config.php but I guessed you meant wp-config.php) but it doesn’t work. Maybe there’s something in my wp-config.php that stops it working?

    • Dean Andrews
      May 13, 2014 (1:56 pm)
      Reply

      Hi, You are correct it is the wp-config.php, I have corrected the instructions. I have also updated the code to be compatible with WordPress SEO version 1.5.3 so you should now see the data being displayed correctly.

  • Joe Connor
    May 14, 2014 (10:08 am)
    Reply

    Hi Dean,
    It works perfectly and now I have the exact information I wanted. I appreciate the time and effort you’ve taken to update the tool, thanks very much! Regards Joe.

  • Nick Wats
    May 14, 2014 (4:14 pm)
    Reply

    Hi Dean,
    This is an excellent job – thank you for creating this tool – just perfect for what I wanted to do

    • Dean Andrews
      May 14, 2014 (5:54 pm)
      Reply

      Thanks Nick, glad you found it useful.

  • Tammi L. Coles
    June 20, 2014 (12:28 pm)
    Reply

    Have you also created a way of importing modified data back into the Yoast plugin?

    • Dean Andrews
      June 20, 2014 (12:40 pm)
      Reply

      Hi Tammi
      The code is to purely export the focus keyword for analysis or import into third party software. If you need to modify data in bulk then you could use the ‘Bulk Title Editor’ & ‘Bulk Description Editor’ built into Yoast WordPress SEO.

  • Matthias
    September 12, 2014 (12:49 pm)
    Reply

    Hi,
    I just tried your php-file, but I just got the header as described above. Might it be, that there came some changes with the latest updates of WordPress (4) or the Yoast plugin?
    Would be great if you could check it again. Tried it with wp3.9.2 and 4.0

    • Dean Andrews
      September 12, 2014 (1:20 pm)
      Reply

      Hi,
      I have just quickly tested on WordPress 4.0 with Yoast SEO 1.6. Are you on the latest Yoast SEO plugin?

      • Matthias
        September 12, 2014 (2:10 pm)
        Reply

        I tried it on 2 different pages and updated everything. So it’s WP4 and Yoast 1.6 now, but still don’t get the list. File is in the same folder as the wp-config.php

        • Dean Andrews
          September 16, 2014 (10:59 am)
          Reply

          Hi Matthias,
          I have emailed you back however I have tested the code against a fresh install of WordPress 4.0 and Yoast SEO 1.6, no other plugins or themes active on this install and everything worked fine. I would suggest that you enable the default theme first, test against that. If that fails to resolve the issue then disable the plugins one at a time (except Yoast SEO) and test. Let me know how you get on please.


Got something to say?

Some html is OK