Ok, so here's what is going on...
I am pulling a list of the most popular post titles from Google Analytics and I'm placing them in an array
$thisnumber = 0;
$start = date('Y-m-d', (time() - (60 * 60 * 24 * 30)));
$end = date('Y-m-d');
$limit = $getnumber;
$titles = array();
$login = new GADWidgetData();
$ga = new GALib($login->auth_token, $login->account_id, 60);
$pages = $ga->pages_for_date_period($start, $end);
foreach($pages as $page) :
$title = $page['children']['value'];
$titlearray = explode(' |', $title, -1);
$titlesub = implode( "", $titlearray);
$thetitles[] = $titlesub;
$thisnumber++;
if($thisnumber > ($getnumber*2)) break;
endforeach;
$titles_list = implode( ",", $thetitles);
So after all of this I am left with both an array of titles in $thetitles and a list of the titles stored in $titles_list
now I need to send all of these titles to a full wordpress query - so I have a function:
function get_post_by_title($page_title, $output = OBJECT) {
global $wpdb;
$post = $wpdb->get_var( $wpdb->prepare( "
SELECT wposts.*
FROM $wpdb->posts wposts
WHERE post_title = %s
AND post_type='post'
AND post_status = 'publish'", $page_title ));
if ( $post )
return get_post($post, $output);
return null;
}
Unfortunately, this can be a bunch of queries, and on a heavily traveled site this could bog things down. I attempted to use the SQL IN function, but I think I'm doing it wrong
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE post_title IN ($titles_list)
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'";
$pageposts = $wpdb->get_results($querystr, OBJECT);
Can anyone with better SQL/Wordpress foo give me a hand?