Impossible to get Attachments Outside WordPress?

My page (outside the wordpress install) begins something like this:

global $wpdb, $wp_query; 
define('WP_USE_THEMES', false);
include_once 'word/wp-blog-header.php';

$args = array(
  'posts_per_page'  => 10,
  'post_type'       => 'post'
);
$queryObject = new WP_Query($args);

I start the loop like so:

Read More
<?php while (have_posts()): the_post(); ?>

In the loop I call this:

<?php the_post_thumbnail();?>

But nothing is returned.

print_r(get_post(4));

(Where 4 is the id of an attachment) also returns nothing.

Calling

<?php the_content(); ?>

echoes the post content, minus the gallery – which also doesn’t get rendered, but the shortcode is removed.

Inside wordpress everything works fine, outside it seems that attachments are simply ignored. Why does this happen? Is there a workaround? Is there something wrong with my query?

.

.

.

I found an alternative solution to my original problem – using a subdirectory, but I still feel this is a valid question. The original is here:

I’m using the wordpress engine as a CMS for a website that can’t be integrated into wordpress right now because of time constraints (but will be eventually), so I need to get hold of things like post thumbnails outside of the wordpress directory. I’m using the following to set up my loop:

global $wpdb, $wp_query; 
define('WP_USE_THEMES', false);
include_once 'word/wp-blog-header.php';
$args = array(
  'posts_per_page'  => 10,
  'post_type'       => 'leather_swatch'
);
$loop = new WP_Query($args);

if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post();

However, in index.php this works, but returns nothing in my external loop:

$thumb = get_post_thumbnail_id($post->ID);
echo '<img src="'.wp_get_attachment_url($thumb).'" />';

What I find really odd is that

print_r(get_post($thumb)); 

returns a post object inside the wordpress directory, but nothing in my custom loop – it doesn’t recognise attachments. However I can still print out the content of my post type in the custom loop using the standard the_content() function.

Can anyone illuminate what’s going on/how I might fix this? I don’t really want to use custom meta data to replicate standard functionality.

Related posts

1 comment

  1. Your older code seems to work just fine, if there were issues with it – something else caused them.

    Your updated version seems to take a step back since your loop is written like main loop and is not using $queryObject that you set up.

Comments are closed.