Created
November 14, 2011 18:04
-
-
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.)
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
//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; | |
} |
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
///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