¿Cómo publicar publicaciones entre una fecha y hoy?

9

¿Es una forma de publicar publicaciones entre una fecha y hoy con query_posts() ?

Ejemplo: todas las publicaciones publicadas desde 2012-04-01

Gracias

EDITAR:

¿Cómo agregar la fecha del filtro en esta consulta de publicaciones?

query_posts( array(  
    array('post'),
    'tax_query' => array(
        array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => array('post-format-image')
        )
    ),
    'cat' => '-173',
    'post_status' => 'publish'
) );
    
pregunta Steffi 14.05.2012 - 14:59

2 respuestas

20

ACTUALIZACIÓN 23 de diciembre de 2014

Hay un método mejor que usa la propiedad date_query de la clase WP_Query :

$args = array(
    'post_type' => 'post', 
    'tax_query' => array(
        array( 
            'taxonomy'  => 'post_format',
            'field'     => 'slug',
            'terms'     => array( 'post-format-image' )
        )
    ),
    'cat'           => '-173',
    'post_status'   => 'publish',
    'date_query'    => array(
        'column'  => 'post_date',
        'after'   => '- 30 days'
    )
);
$query = new WP_Query( $args );

ANTIGUA RESPUESTA

Utilice los Parámetros de tiempo en WP_Query ()

Ejemplo de cotización del Códice:

Devolver publicaciones de los últimos 30 días:

// This takes your current query, that will have the filtering part added to.
$query_string = array(
    'post_type' => 'post', 
    'tax_query' => array(
        array(
            'taxonomy'  => 'post_format',
            'field'     => 'slug',
            'terms'     => array( 'post-format-image' )
        )
    ),
    'cat'           => '-173',
    'post_status'   => 'publish'
);

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
    // posts in the last 30 days
    $where .= " AND post_date > '" . date( 'Y-m-d', strtotime( '-30 days' ) ) . "'";
    return $where;
}

add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );

Editar (en respuesta a la pregunta actualizada del OP).

Evite el uso de query_posts . Puede usar la técnica anterior para modificar su consulta principal (sujeto a algunos condicionales - es la página de inicio, es una página llamada 'foobar 'etc.):

function wpse52070_filter_where( $where = '' , $query ) {
   if( $query->is_main_query() && is_page( 'foobar' ) ){
      // posts in the last 30 days
      $where .= " AND post_date > '" . date( 'Y-m-d', strtotime( '-30 days' ) ) . "'";
   }

    return $where;
}
add_filter( 'posts_where', 'wpse52070_filter_where' );
    
respondido por el moraleida 14.05.2012 - 15:08
3

A partir de 3.7 puede usar date_query enlace

Así que los argumentos pasados se verían así:

$query_string = array(
      'post_type' => 'post', 
      'date_query' => array(
        'after' => '2012-04-01' 
      ),
      'tax_query' => array(
          array( 
             'taxonomy' => 'post_format',
             'field' => 'slug',
             'terms' => array('post-format-image')
          )
      ),
      'cat' => '-173',
      'post_status' => 'publish'
);
    
respondido por el Kode 31.07.2014 - 23:41

Lea otras preguntas en las etiquetas