php - 在给定类别中使用通配符搜索 WordPress 帖子中的特定句子并输出唯一结果

标签 php mysql wordpress

例如,我的 WordPress 安装中有一个金属类别,在不同帖子的该类别中到处发布了不同的金属名称,例如金属(黄铜)金属(铝)等。

我需要的是输出该类别的所有帖子中所有金属的所有唯一提及,跳过特定的帖子。

据我所知,我应该创建一个包含所有金属提及的数组,按照我想要的方式对其进行排序,仅保留唯一的名称,然后使用输出数组的内容

<?php foreach($my_array as $key => $value) {echo $value.', ';} ?>

使用类似的方法准备最终数组对我来说没有问题

<?php $my_array = array_unique(($metals_array), SORT_REGULAR); ?>

但我不知道如何用所需的数据填充该数组,搜索 Metal (*) 的帖子,并跳过,例如,Metal (steel)输出。

最佳答案

请尝试这个:

<?php 
$title_array = array();
$args = array('post_type' => 'post', 'category_name' => 'metals', 'orderby'=> 'title', 'order'=>'ASC' );
$query = new WP_Query($args); 
if($query->have_posts()): 
    while($query->have_posts()) : $query->the_post(); 
        $title_array[] = get_the_title();
    endwhile; 
endif;
$unique_array = array_unique($title_array);

if($unique_array):
    foreach($unique_array as $unique) {
        echo $unique;
    }
endif;
?>

这是基于类别的简单帖子的查询,但如果您创建了“自定义帖子类型”,那么查询将会更改,否则它将适合您。

'category_name' => 'category-slug'。类别名称由类别别名组成。

希望这对您有帮助。

关于php - 在给定类别中使用通配符搜索 WordPress 帖子中的特定句子并输出唯一结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42146330/

相关文章:

javascript - 在 3 个 div 之间拆分单个字符串(常规文本,无 HTML)

php - 字符在 mysql 中显示为乱码(希伯来语)

php - 如何在OOP中访问数据库类?

java - JPA : Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1 上的问题

mysql - spring boot和hibernate mysql数据库连接一直断开

c# - MySQL Entity Framework - 当多个元素作为参数传递时,Contains 方法会引发异常

php - 将自定义 ul 注入(inject)菜单

使用 preg_replace 和 FOR 循环替换 PHP 关键字

PHP SQL 每 9 个月输入一次 null

html - 使用媒体查询更改页面内容 (WordPress)