为什么我在WordPress中使用get_posts只获得一篇文章?


Why I'm getting only one post with get_posts in WordPress?

更新:我现在知道为什么不工作了。那是因为我想展示一个私人帖子。蠢的我。发生在我们最好的人身上。对不起。谢谢你的帮助。

我试图列出所有的帖子与一定的元键和元值,但我不能得到所有的帖子与一定的元值。我只有一个。

我有一个foreach循环,下一个代码列出我所有的帖子,像这样:

$args = array( 
        'post_type' => 'produs', 
        'meta_key' => 'sticky_post', 
        'meta_value' => 1
); 
$posts = get_posts($args);

          <?php
							foreach($posts as $post){ ?>
                                <div class="product" id="product-<?php echo $post->ID; ?>">
                                    <div class="thumb new_product_thumb">
                                    <?php 
                                        $thumb_args = array('class' => 'product-img', 'alt' => the_title_attribute( 'echo=0' ) ); 
                                        ?>
                                    <a class="product-thumb read-more" style="" href="<?php echo the_permalink(); ?>" title="citește mai departe">
                                        <?php echo the_post_thumbnail('product-listing', $thumb_args); ?>
                                    </a>
                                     <?php  //if ( current_user_can('manage_options') ) {
                                                if (get_field('tva_produs',$post->ID) == '9') echo '<div class="tva_redus"></div>';
                                                // var_dump($top_sellers_new);
                                            //}
                                     ?>
                                    </div>
                                    <h2>
                                        <a class="read-more" style="" href="<?php echo the_permalink(); ?>" title="citește mai departe">
                                            <?php the_title(); ?>
                                        </a>
                                    </h2>
                                    <div class="product-content">
                                        Pret: <?php echo get_field('pret_nou', $post->ID); ?> lei
                                    </div>
                                    <a class="read-more more2" href="<?php echo the_permalink(); ?>" title="citește mai departe"><p>Detalii produs</p> <span class="arrow-next">&nbsp;</span></a>
                                    <?php $id = get_the_ID();?>
                                    <form id="adauga_in_cos" action="<?php echo THEME_URL; ?>/product.php?action=add&product=<?php echo $id; ?>" method="post">
                                        <input type="hidden" name="produs_id" id="produs_id" value="<?php echo $id; ?>" />
                                        <input type="hidden" name="produs_price" id="produs_price" value="<?php echo get_field( 'pret_nou', $id ) ?>" />
                                        <input type="hidden" name="produs_name" id="produs_name" value="<?php echo $post->post_title; ?>" />
                                        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="add_to_sc" id="add-<?php echo $id; ?>"></a>
                                    <!-- <input id="adauga_but" class="add_to_sc" type="submit" value="Adauga in cos" />-->
                                    </form>
                                    <div class="horizontal"></div>
                                </div>
                         <?php } ?>

为什么我只得到一个帖子与meta_value 1,而不是所有与meta_value 1 ?

这里有三个问题

  • 不要滥用$posts全局,你正在使用它作为一个自定义变量,所以你打破了全局。使用自定义变量名,例如' $posts_array '。

  • 如果你要使用$post (,你应该使用它来设置postadat),你应该把它重置回原来的值

  • posts_per_page设置为-1,获取所有具有所需自定义字段的帖子

你的代码应该看起来像这样:(未测试)
$args = array( 
    'post_type' => 'produs', 
    'meta_key' => 'sticky_post', 
    'meta_value' => 1,
    'posts_per_page' => -1
); 
$posts_array = get_posts($args);
foreach ( $posts_array as $post ) {
    setup_postdata( $post );
    // All your other code
}
wp_reset_postdata();

如果这不起作用,您应该确保您没有pre_get_posts动作的坏实例

有没有试过添加

'numberposts'       => -1,

这应该使get_post获得所有可用的帖子,如果这不起作用,我们无法进一步帮助你,除非你提供更多的代码

试试这个,您可以使用posts_per_page参数,并将其设置为-1

$args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' =>'any', 'post_parent' => $post->ID ); 
$attachments = get_posts( $args );

请尝试使用WP_Query而不是get_posts

如果您只需要一个帖子数组,而不需要查询对象-使用get_posts()。否则,如果您确实需要访问查询对象方法、分页或顶部的粘性帖子,则应该使用WP_Query。

通常(默认使用WP_Query对象)- WordPress查询总共有多少篇文章-即使你只在前10篇之后。它这样做是为了执行分页。因此,get_posts()实际上(稍微)更快(它也忽略粘性帖子)。

'meta_value' -注意,'meta_key=keyname'也必须出现在查询中。还请注意,排序将按字母顺序排列,这对于字符串(即单词)是可以的,但对于数字(例如1,3,34,4,56,6等,而不是您可能自然期望的1,3,4,6,34,56)可能是意想不到的。

$args = array( 
        'post_type' => 'produs', 
        'meta_key' => 'sticky_post', 
        'meta_value' => 1,
        'posts_per_page' => -1
); 
$myposts = get_posts($args);
foreach ( $myposts as $post ) : setup_postdata( $post ); ?> 
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>   
<?php endforeach; wp_reset_postdata(); ?>