¿Roto? WP_Query y "archivo adjunto" como tipo de publicación

14

Tengo una galería adjunta a una página. En esa página, estoy ejecutando la siguiente consulta:

$events_gallery = new WP_Query( // Start a new query for our videos
array(
    'post_parent' => $post->ID, // Get data from the current post
    'post_type' => 'attachment', // Only bring back attachments
    'post_mime_type' => 'image', // Only bring back attachments that are images
    'posts_per_page' => '3', // Show us the first three results
    'status' => 'inherit', // Inherit the status of the parent post 
    'orderby' => 'rand', // Order the attachments randomly  
    )
);

He experimentado varias formas y, por alguna razón, no puedo devolver los archivos adjuntos. ¿Me estoy perdiendo algo obvio aquí?

Actualizar*

Gracias a Wok por señalarme en la dirección correcta.

Resulta que estaba usando "status" en lugar de "post_status". El códice había usado "estado" como ejemplo en su explicación en contexto del tipo de publicación "adjunto". Actualicé el códice para hacer referencia a "post_status" en su lugar. El código correcto es el siguiente:

$events_gallery = new WP_Query( // Start a new query for our videos
array(
    'post_parent' => $post->ID, // Get data from the current post
    'post_type' => 'attachment', // Only bring back attachments
    'post_mime_type' => 'image', // Only bring back attachments that are images
    'posts_per_page' => '3', // Show us the first result
    'post_status' => 'inherit', // Attachments default to "inherit", rather than published. Use "inherit" or "all". 
    'orderby' => 'rand', // Order the attachments randomly  
    )
);  
    
pregunta Jonathan Wold 20.04.2011 - 00:37

4 respuestas

13

Estos son los parámetros de consulta que uso ... funciona para mí cuando recorro los resultados

array(
                'post_parent' => $post->ID,
                'post_status' => 'inherit',
                'post_type'=> 'attachment',
                'post_mime_type' => 'image/jpeg,image/gif,image/jpg,image/png'                  
            );
    
respondido por el Wok 20.04.2011 - 02:57
10

Agregue $args , es importante.

'post_status' => 'any'

No: 'post_status' => null

Esto es importante porque los archivos adjuntos no tienen un post_status , por lo que el valor predeterminado para post_status , published , no encontrará archivos adjuntos.

    
respondido por el Pham 19.09.2013 - 06:10
0

Mirando la consulta que genera, parece ser una especie de error. 'estado' = > "heredar" se interpreta como el estado del padre, cuando la entrada en la base de datos del adjunto es literalmente "heredar".

Una alternativa es usar get_children en lugar de WP_Query.

    
respondido por el Milo 20.04.2011 - 01:13
0

He podido mostrar todas las imágenes que son adjuntos a una publicación usando este código.

<?php
$args = array( 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
    if ($attachments) {
    foreach ( $attachments as $attachment ) { ?>
      <img src="<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>" />
<?php   }
    } ?>

Y para repetir la URL de la imagen original a tamaño completo, puedes vincular esa imagen a

<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>

Esperamos que este sea un enfoque de lo que estás tratando de hacer.

    
respondido por el Chad Von Lind 20.04.2011 - 02:16

Lea otras preguntas en las etiquetas