Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按自定义值字段分组wordpress posts_Php_Wordpress - Fatal编程技术网

Php 按自定义值字段分组wordpress posts

Php 按自定义值字段分组wordpress posts,php,wordpress,Php,Wordpress,我有几篇文章都有一个名为“series”的自定义字段。我想按此自定义字段对所有帖子进行分组,并在下面列出所有没有此自定义字段的帖子 首先,我希望获得分组的自定义字段值,然后能够再次查询具有此自定义值键和值的所有帖子。但是,即使尝试获取唯一的自定义值也不起作用 我尝试的是: <?php function query_group_by_filter($groupby){ global $wpdb; return $wpdb->postmeta .

我有几篇文章都有一个名为“series”的自定义字段。我想按此自定义字段对所有帖子进行分组,并在下面列出所有没有此自定义字段的帖子

首先,我希望获得分组的自定义字段值,然后能够再次查询具有此自定义值键和值的所有帖子。但是,即使尝试获取唯一的自定义值也不起作用

我尝试的是:

<?php
    function  query_group_by_filter($groupby){
       global $wpdb;

       return $wpdb->postmeta . '.meta_key = "series"';
    }
?>

<?php add_filter('posts_groupby', 'query_group_by_filter'); ?>

<?php $states = new WP_Query(array(
    'meta_key' => 'series',
    'ignore_sticky_posts' => 1
)); 
?>

<?php remove_filter('posts_groupby', 'query_group_by_filter'); ?>


<ul>
<?php
while ( $states->have_posts() ) : $states->the_post();

$mykey_values = get_post_custom_values( 'series' );

foreach ( $mykey_values as $key => $value ) {
    echo "<li>$key => $value ( 'series' )</li>"; 
}   

endwhile;
?>
</ul>

    具有自定义值的所有帖子:

    <?php
    $args = array(
        'post_type' => 'my-post-type',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'series',
                'value' => 'my-val',
                'compare' => '='
            ),
            array(
                'key' => 'series',
                'value' => '',
                'compare' => '!='
            )
        )
    );
    
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) : ?>
    <ul>
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <li id="post-<?php the_ID(); ?>">
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </li>
        <?php
        endwhile;
        wp_reset_postdata(); ?>
    </ul>
    <?php endif; ?>
    
    
    

      谢谢您!但是第一部分(带值的查询)不是我直接搜索的内容。我需要一个由自定义字段首先分组。例如,按自定义字段“系列”分组,假设我有带有值“Development series”和“Cloud series”的帖子,然后我想按这两个变量自定义字段值分组,然后在这些组下面显示相关帖子。
      <?php
      $args = array(
          'post_type' => 'my-post-type',
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'meta_query' => array(
              array(
                  'key' => 'series',
                  'value' => '',
                  'compare' => '='
              )
          )
      );
      
      $the_query = new WP_Query( $args );
      if ( $the_query->have_posts() ) : ?>
      <ul>
          <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
              <li id="post-<?php the_ID(); ?>">
                  <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
              </li>
          <?php
          endwhile;
          wp_reset_postdata(); ?>
      </ul>
      <?php endif; ?>