Es posible que cuando estas desarrollando un plugin necesites listar entradas o productos. Por ejemplo para adaptar una plantilla que no tiene soporte para woocommerce para que lo tenga.
Una forma sencilla de hacerlo es con este código PHP. Puedes incluirlo y adaptarlo dentro de tu plugin/theme:
$args = array( 'p' => $id,'post_type' => 'any' );
$consulta = new WP_Query( $args );
if( ! empty( $consulta ) && $consulta -> have_posts() ){
$consulta->the_post();
$return_html .= "
ID: ".get_the_ID();
$return_html .= "
Post_type: ".$consulta->post->post_type;
$return_html .= "
Post_name: ".$consulta->post->post_name;
$return_html .= "
Title: ".Get_the_title();
$thumbnail_url = esc_url( get_the_post_thumbnail_url());
$return_html .= "
Thumbnail URL: $thumbnail_url";
$return_html .= '
Thumbnail:
';
$return_html .= "
Permalink: ".esc_url( get_permalink() );
}
/*
while ($consulta -> have_posts()) :
$consulta->the_post();
$return_html .= "---".get_the_ID()."---";
$return_html .= "---".esc_url( get_permalink() )."---";
$return_html .= Get_the_title();
$return_html .= Get_the_post_thumbnail_url();
if( has_post_thumbnail() ) {
$return_html .= the_post_thumbnail('thumbnail-featured');
}
$return_html .= the_title( '', '
' );
endwhile;
*/
wp_reset_postdata();
return $return_html;
Este fragmento de código es util sobre todo porque se puede discriminar el tipo de post, es decir en la primera linea donde aparece ‘post_type’ => ‘any’ podemos sustituir any por el tipo correspondiente. Tambien extrae el link del thumnail para que puedas insertarlo en tu representación de la entrada.