WordPress通过元数据消息分页获取和排除


WordPress get by and exclude by meta data messes pagination

所以这是我的第一篇帖子,哇,我不知道这个网站的存在。我到处看了看问题,我希望我的问题不是一个接一个的愚蠢问题。虽然我是一个傻瓜:S

好的,所以我在WordPress中创建了一个功能,它将在新的帖子页面上添加一个元框,这样我就可以指定是否应该对这篇帖子进行专题介绍(我读到这比为SEO目的创建一个专题类别更好?)。

不管怎样。。我使用的代码显示了最新的。这是它的代码:

<?php
$args=array(
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  while ($my_query->have_posts()) : $my_query->the_post();
  $custom = get_post_meta($my_query->post->ID, '_featuredpost_meta_value_key', true);
if (  $custom ){
?>
<article class="container" itemprop="blogPosts" itemscope itemtype="http://schema.org/BlogPosting">
    <div class="row">
        <h2 itemprop="about">
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </h2>
    </div>
    <div class="row">
        <div class="<?php if ( has_post_thumbnail() ) { ?>two-thirds column<?php } else {?> twelve columns  <?php } ?>">
            <p class="post-excerpt"><?php  modified_excerpt(); ?></p>
        </div>
    <?php if ( has_post_thumbnail() ) { ?>
        <div class="one-third column">
            <?php the_post_thumbnail('full', array('class'=>'hide-mobile')); ?> 
        </div>
    <?php } ?>      
    </div>
    <div class="row">
        <a href="<?php the_permalink(); ?>" class="button button-primary">Continue Reading</a>
    </div>
    <hr />
    <div class="post-info">
        <ul>
            <li class="date"><?php the_date();?></li>
            <li class="author"><a href="<?php bloginfo('url'); ?>/questions/user/<?php echo get_the_author_meta('user_login'); ?>"><?php echo get_the_author_meta('display_name'); ?></a></li>
            <li class="category"><?php the_category(', '); ?></li>
            <li class="tags"><?php the_tags('',', ',''); ?></li>
        </ul>
    </div>
</article>
  <?php
}
endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>

现在,当我使用下面相同的代码,但随后使用:

 if ( ! $custom ){

以显示未设置为同样有效的特色的帖子。问题是分页不再有效。当我转到第二页时,它只是复制了主页上的内容。

这让我相信我已经创建了一个拼凑在一起的糟糕代码。有人能帮我建立一个循环吗?它将排除元数据_featuredpost_meta_value_key设置为的任何帖子。

提前感谢

您将希望在原始$args数组中使用WP元查询。

https://codex.wordpress.org/Class_Reference/WP_Meta_Query

从文档中,这里有一个例子:

    $meta_query_args = array(
        'relation' => 'OR', // Optional, defaults to "AND"
        array(
            'key'     => '_my_custom_key',
            'value'   => 'Value I am looking for',
            'compare' => '='
        )
    );
    $meta_query = new WP_Meta_Query( $meta_query_args );

但是,您也可以使用WP_Query类提供的糖,并将其作为meta_query值传递给原始args:

    $args=array(
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1,
      'meta_query' => array(
        'key'     => '_featuredpost_meta_value_key',
        'value'   => 'Yes',
        'compare' => '='
      )
    );