Products
GG网络技术分享 2025-03-18 16:12 3
DB newbie hack here. I slogged my way through coding up a WP template file that retrieves the post data from another site\'s WP DB. It worked and was functioning fine for about 2 years and suddenly stopped working - crashing and not allowing the page to finish loading. Getting an \'The site is experiencing technical difficulties.\' error.
I commented out the ::prepare method to try and isolate the issue and the page finished loading but still no data from the posts... again full novice here so I\'m sure a seasoned vet could probably see the issue. Thanks for any insight!
<?php$originaldb = new wpdb(\'XXXXXX\',\'XXXXXX\',\'XXXXXX\',\'XXXXXXXX\');
//Home Recent POST
$post_id_recent = get_field(\'recent_post_id\');
$prep_recent = $originaldb->prepare(\"SELECT * FROM wp_posts WHERE $wpdb->posts.ID = $post_id_recent\");
$recent = $originaldb->get_results($prep_recent);
$prep_recent_thumb = $originaldb->prepare(\"
SELECT p1.*, wm2.meta_value FROM wp_posts p1 LEFT JOIN
wp_postmeta wm1 ON (
wm1.post_id = $post_id_recent
AND wm1.meta_value IS NOT NULL
AND wm1.meta_key = \'_thumbnail_id\'
)
LEFT JOIN wp_postmeta wm2 ON (wm1.meta_value = wm2.post_id AND wm2.meta_key = \'_wp_attached_file\'
AND wm2.meta_value IS NOT NULL) WHERE p1.post_status=\'publish\' AND p1.post_type=\'post\'
\");
$recent_thumb = $originaldb->get_results($prep_recent_thumb);
foreach ($recent_thumb as $obj) :
$url = $obj->meta_value;
endforeach; ?>
<?php foreach ($recent as $obj) : ?>
<div class=\"home_recent_thumb\" style=\"background-image:url(<?php echo \'http://kuteblackson.com/blog/wp-content/uploads/\'.$url; ?>)\" onclick=\"location.href=\'<?php echo $obj->guid; ?>\';\"></div>
<div class=\"home_recent_content\">
<div class=\"home_recent_title\"><a href=\"<?php echo $obj->guid; ?>\"><?php echo $obj->post_title; ?></a></div>
<div class=\"home_recent_excerpt\"><?php echo $obj->post_excerpt; ?></div>
<a href=\"<?php echo $obj->guid; ?>\" class=\"button medium radius home_blog_button\">READ BLOG</a><br>
<a href=\"blog\" style=\"font-size: 16px; color: #9e9898; text-decoration:underline;\">See all blog posts</a>
</div><!-- end .home_recent_content -->
<div style=\"clear:both;\"></div>
<?php endforeach; wp_reset_query(); ?>
You\'re not using the prepare() method correctly.
From the documentation:
Parameters
$query (string) (Required) Query statement with sprintf()-like placeholders
$args (array|mixed) (Required) The array of variables to substitute into the query\'s placeholders if being called with an array of arguments, or the first variable to substitute into the query\'s placeholders if being called with individual arguments.
$args,... (mixed) (Required) further variables to substitute into the query\'s placeholders if being called wih individual arguments.
In your code, both calls to the prepare()
method are passing only the first parameter (the query) and the method expects at least two of them which is why the code is triggering an error and so you get that \\\"The site is experiencing technical difficulties\\\" message.
This is how your prepare()
calls should look like:
$prep_recent = $originaldb->prepare(\\\"SELECT * FROM wp_posts WHERE $wpdb->posts.ID = %d\\\",
array($post_id_recent)
);
$prep_recent_thumb = $originaldb->prepare(\\\"SELECT p1.*, wm2.meta_value FROM wp_posts p1 LEFT JOIN
wp_postmeta wm1 ON (
wm1.post_id = %d
AND wm1.meta_value IS NOT NULL
AND wm1.meta_key = \'_thumbnail_id\'
)
LEFT JOIN wp_postmeta wm2 ON (
wm1.meta_value = wm2.post_id
AND wm2.meta_key = \'_wp_attached_file\'
AND wm2.meta_value IS NOT NULL
)
WHERE p1.post_status=\'publish\' AND p1.post_type=\'post\'\\\",
array($post_id_recent)
);
Notice that the $post_id_recent
variable is being passed to the prepare()
method as the second parameter in both cases. This should fix the issue (assuming that\'s the only problem.)
Demand feedback