I have a CMS setup for a client with three meta boxes for related content. All the client has to do is eneter a page slug (one in each) and the site will return the three related products.
Everything was working fine until my client accidentally misspelled one of the slugs. Instead of returning nothing for this spot, WordPress returns about 6 random items.
IN FUNCTIONS.PHP:
function get_ID_by_page_name($page_name) {
global $wpdb;
$page_name_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."' AND post_type = 'page'");
return $page_name_id;
}
IN THE TEMPLATE FILE:
$goesWith = get_post_meta($post->ID, 'goes_with', true);
if (($goesWith) or ($goesWith2) or ($goesWith3)) {
echo "<div class='goes-with'>";
echo "<h2>Goes Great With:</h2>";
// OPTION ONE
$pageID = get_ID_by_page_name($goesWith);
if ($goesWith) {
$args = array(
'post_type' => 'page',
'page_id' => $pageID,
'post_status' => 'publish'
);
query_posts( $args );
while(have_posts()) {
the_post(); // vital
echo "<div class='post-product'>";
echo "<a href=";
the_permalink();
echo ">";
thumbPath(140, 140);
echo "</a><a href=";
the_permalink();
echo "><p>";
the_title();
echo "</p></a></div><!--end post-product-->";
}
}
else {
echo "";
}
wp_reset_query();
First of all, you don't need any custom code to do what you're doing:
$goes_with = get_post_meta(get_the_ID(), 'goes_with'); //Returns array when 3rd parameter is left empty or false
foreach($goes_with as $slug){
$args = array(
'post_type' => 'page',
'post_status' => 'publish',
'pagename' => $slug
);
query_posts( $args ); //Bad way
while(have_posts()){ the_post(); //Bad way
//$query = new WP_Query($args); //Good way
//while($query->have_posts()){ $query->the_post(); //Good way
echo "<div class='post-product'>";
echo "<a href='".get_permalink()."'>";
thumbPath(140, 140);
echo "</a><a href='".get_permalink()."'><p>".get_the_title()."</p></a></div><!--end post-product-->";
}
wp_reset_query(); //Bad way
//wp_reset_postdata(); //Good way
}
Secondly, you should absolutely avoid using query_posts(). It's probably too late as your theme has already been shipped off to your client, but you should consider switching everything from query_posts to WP Query if putting the extra time into your theme isn't a problem.
There's a measurable performance increase using WP_Query, as well as the fact that that it doesn't alter any of the Wordpress Globals.
If you want to see how it's implemented, feel free to comment out all lines labeled "Bad way" and uncomment all lines labeled "Good way".
Also, with the above code, you can reuse the same 'goes_with' key in your Custom Meta Fields to link as many other pages as you need to that given page query. Since you're running the query by pagename rather than ID, this particular approach will have to be made from within a loop.
The benefit to this over your original, however, is that you're relying on the built-in functionality of Wordpress to make the query for you rather than making extra calls to the database to return results that you already had available to you.
This code is untested and will probably need some refactoring in order to make it work with your setup, but it should get you started.
Hope this helps you out, and good luck.