Skip to content

Instantly share code, notes, and snippets.

@carlynorama
Created November 14, 2011 18:04
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 carlynorama/1364627 to your computer and use it in GitHub Desktop.
Save carlynorama/1364627 to your computer and use it in GitHub Desktop.
Wordpress: SQL Query to WP_Query Educational Fail (post__in can bite me.)
//These are in the Functions.php file
//gist-1364599
function objectToArray($result)
{
$array = array();
foreach ($result as $key=>$value) {
if (is_object($value)) {
$array[$key]=objectToArray($value);
}
elseif (is_array($value)) {
$array[$key]=objectToArray($value);
}
else {
$array[$key]=$value;
}
}
return $array;
}
// turn an array-of-arrays with a bottom key of 'ID' to a list
function flattenObjectByID($object)
{
$idlist = array();
foreach ($object as $val) {
$myid = $val['ID']; // does this work?
array_push($idlist, $myid);
}
return $idlist;
}
function flattenObjectByKey($object, $bk)
{
$idlist = array();
foreach ($object as $val) {
$myid = $val[$bk]; // does this work?
array_push($idlist, $myid);
}
return $idlist;
}
///In showcase.php (or wherever.php)
//get the sticky_posts
$sticky = get_option( 'sticky_posts' );
//get id's only of child pages
$local_children = $wpdb->get_results( "SELECT ID FROM wp_posts WHERE post_parent = $post->ID AND post_type='page'" );
//compare the results
print_r($sticky);
print_r($local_children);
$array = objectToArray($local_children);
$flattenedbyID = flattenObjectByID($array);
$flattenedbyPassedKey = flattenObjectByKey($array, 'ID');
print_r($sticky);
print_r($flattenedbyID);
print_r($flattened2);
if ( ! empty( $flattenedbyPassedKey ) ) :
$featured_args = array(
//post__in THIS DOES NOT WORK B/C post__in only works with POSTS!!!
'post__in' => $flattenedbyPassedKey,
'post_status' => 'publish',
'posts_per_page' => 10,
'no_found_rows' => true,
);
// The Featured Posts query.
$featured = new WP_Query( $featured_args );
print_r($flattened);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment