POST_PER_PAGE-1仅返回一半的结果

时间:2013-10-14 作者:Sergio del Amo

我有一个在函数中创建的自定义帖子类型。php。

function codex_custom_init() {
  $labels = array(
    \'name\'               => \'Restaurantes\',
    \'singular_name\'      => \'Restaurante\',
    \'add_new\'            => \'Añadir Nuevo\',
    \'add_new_item\'       => \'Añadir Nuevo Restaurante\',
    \'edit_item\'          => \'Editar Restaurante\',
    \'new_item\'           => \'Nuevo Restaurante\',
    \'all_items\'          => \'Todos los Restaurantes\',
    \'view_item\'          => \'Ver Ver Restaurante\',
    \'search_items\'       => \'Buscar Restaurantes\',
    \'not_found\'          => \'No se encontraron restaurantes\',
    \'not_found_in_trash\' => \'Restaurantes no encontrados en la Papelera\',
    \'parent_item_colon\'  => \'\',
    \'menu_name\'          => \'Restaurantes\'
  );

  $args = array(
    \'labels\'             => $labels,
    \'public\'             => true,
    \'publicly_queryable\' => true,
    \'show_ui\'            => true,
    \'show_in_menu\'       => true,
    \'query_var\'          => true,
    \'rewrite\'            => array( \'slug\' => \'restaurante\' ),
    \'capability_type\'    => \'post\',
    \'has_archive\'        => true,
    \'hierarchical\'       => false,
    \'menu_position\'      => null,
    \'supports\'           => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' )
  );

  register_post_type( \'restaurante\', $args );
}
add_action( \'init\', \'codex_custom_init\' );
我试图在存档页中显示属于此自定义帖子类型的所有项目。我有460个这种自定义帖子类型的条目。我创建了文件archive-restaurante.php

我试图按一个名为restaurante_municipio

<?php 
query_posts( 
    wp_parse_args(
         $wp_query->query
        ,array(\'post_type\' => \'restaurante\',
               \'posts_per_page\' => -1, 
               \'meta_key\' => \'restaurante_municipio\',
               \'orderby\' => \'meta_value\',
               \'order\' => \'ASC\')
    )
);
global $wp_query; 
$total_results = $wp_query->found_posts; 
echo \'<p><b>Restaurantes: \'.$total_results.\'</b>\';
?>

if(have_posts() ) {     
?>
...
..
.
<?php while (have_posts()) : the_post(); ?>
...
..
.
<?php endwhile; ?>
...
..
.
显示的项目数为230结果总数(460)除以2。

如果我将posts\\u per\\u页面更改为100,则显示的结果数为50。再次将post\\u per\\u页除以2。

如果我将post\\u per\\u页面更改为920,则显示的结果数再次为230。

我做错了什么?

非常感谢您的帮助。

1 个回复
SO网友:Sergio del Amo

正如评论所暗示的那样,我将query\\u帖子替换为WP\\u query查询。

$args=array(
    \'post_type\' => \'restaurante\',
    \'posts_per_page\' => -1,
    \'meta_key\' => \'restaurante_municipio\',
    \'orderby\' => \'meta_value\',
    \'order\' => \'ASC\');
$the_query = null;
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) {

    $total_results = $the_query->found_posts; 
    echo \'<p><b>Restaurantes: \'.$total_results.\'</b>\';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        ...
        ..
        .
    }
}
wp_reset_postdata();
现在它正在工作

结束

相关推荐