简体   繁体   English

PHP,Wordpress查询,将匹配结果合并为1

[英]PHP, Wordpress Query, Combine Matched Results in 1

I am running a query in Wordpress to get a list of speakers from a custom post type. 我在Wordpress中运行查询以获取自定义帖子类型的发言人列表。 I only need one instance of each speaker returned even though a speaker may show up multiple times as meta for the custom post types. 即使说话者可能多次显示为自定义帖子类型的元数据,我也只需要返回每个发言者的一个实例。

So for example, if I query and get a list back like this: Bob, Bob, Bob, John, Sally, Bob, Bob, John, Bob 例如,如果我查询并获得这样的列表:Bob,Bob,Bob,John,Sally,Bob,Bob,John,Bob

I would want it to be: Bob, John, Sally 我希望它是:Bob,John,Sally

I'm not sure how to group all the matched results with PHP. 我不知道如何用PHP对所有匹配的结果进行分组。

Here is my query: 这是我的查询:

global $post;
  $args = array (
    'post_type' = 'event',
    'posts_per_page' = -1,
    'post_status' => 'publish',
    'fields' => 'ids',
    'meta_query' => array (
      'relation' => 'AND'
      array (
        'key' => 'pt_eventSpeakerType',
        'value' => 'Lead Speaker',
        'compare' => '=',
      ),
    ),
  );
$posts = get_posts($args);
if($posts) :
  foreach($posts as $post):setup_postdata($post);
    echo get_post_meta($post->ID, 'pt_eventSpeaker' true).'<br>';
  endforeach;
endif;
wp_reset_postdata();

Any help is appreciated! 任何帮助表示赞赏!

from what you described, I assume that you are not creating SQL query correctly. 根据您的描述,我假设您没有正确创建SQL查询。 Method get_posts($args); 方法get_posts($args); is probably not returning distinct results, that may be the problem with your duplicated values. 可能不会返回不同的结果,这可能是您的重复值的问题。 I hope it helps. 我希望它有所帮助。

I figured it out. 我想到了。 This may not be the best way, but it works and doesn't require any alteration of the query: I found a PHP array function called array_unique 这可能不是最好的方法,但它可以工作,不需要对查询进行任何更改:我发现了一个名为array_unique的PHP数组函数

First I have to return all the speaker names in a foreach loop and then convert that to an array of its own. 首先,我必须在foreach循环中返回所有发言者名称,然后将其转换为自己的数组。 It gets rid of all the other objects returned by WP_Query . 它摆脱了WP_Query返回的所有其他对象。

Then use the array_unique function to sort those into an array of single entries for each speaker name. 然后使用array_unique函数将这些函数排序为每个发言者名称的单个条目数组。

Finally, using another foreach to loop through those names and output them. 最后,使用另一个foreach这些名称并输出它们。

global $post;
  $args = array (
    'post_type' = 'event',
    'posts_per_page' = -1,
    'post_status' => 'publish',
    'fields' => 'ids',
    'meta_query' => array (
      'relation' => 'AND'
      array (
        'key' => 'pt_eventSpeakerType',
        'value' => 'Lead Speaker',
        'compare' => '=',
      ),
    ),
  );
$posts = get_posts($args);
if($posts) :
  foreach($posts as $post):setup_postdata($post);
    $speakerName = get_post_meta($post->ID, 'pt_eventSpeaker', true);
    $speakers[]= $speakerName;
  endforeach;
  $names = array_unique($speakers);
  foreach($names as $name) :
    echo $name.'<br>';
  endforeach;
else :
  echo 'No Posts Found.';
endif;
wp_reset_postdata();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM