【问题标题】:Wordpress search form and search result through ACF field in custom taxonomy通过自定义分类法中的 ACF 字段的 Wordpress 搜索表单和搜索结果
【发布时间】:2017-06-02 16:21:46
【问题描述】:

所以我觉得我让自己过得很艰难。

我的网站上有一个搜索字段,已分成 3 个,例如:

<input type="text" name="part1" />
<input type="text" name="part2" />
<input type="text" name="part3" />

在我的结果页面上,我创建了搜索字符串,例如:

$searchstring = $part1.'-'.$part2.'-'.$part3;

然后该过程是在数据库中搜索我的自定义字段,其值为$searchstring

我在那里找到了一个搜索功能https://gist.github.com/jserrao/d8b20a6c5c421b9d2a51,我认为它与我想要实现的功能相当接近,但我不确定如何实现所有功能。

我的数据大致是这样的:

(taxonomy) product_cat - (name) Category 1 - (custom field) gc_number - (value I need to search) 77-999-77
(taxonomy) product_cat - (name) Category 2 - (custom field) gc_number - (value I need to search) 73-333-73
(taxonomy) product_cat - (name) Category 3 - (custom field) gc_number - (value I need to search) 76-666-76

然后我需要向用户显示product_cat 名称。

希望这是有道理的,任何帮助将不胜感激!

【问题讨论】:

    标签: php wordpress woocommerce wordpress-theming advanced-custom-fields


    【解决方案1】:

    来自ACF 文档

    $posts = get_posts(array(
        'numberposts'   => -1,
        'post_type'     => 'post',
        'meta_key'      => 'color',
        'meta_value'    => 'red'
    ));
    

    此示例显示了用于查找名为“color”的自定义字段的值为“red”的所有帖子的参数。在您的情况下,您应该使用gc_number 作为 meta_key 和 $searchstring 作为 meta_value。然后你可以使用get_the_terms()来定义帖子属于哪个类别。

    【讨论】:

    • 您好,谢谢。我已经看过了,让我震惊的是查询的帖子类型,它在示例中设置为帖子。但是,我的自定义字段是在分类(product_cat)上设置的。我是否只是将帖子更改为分类学或 product_cat 或其他内容?
    【解决方案2】:

    我在 Stack Exchange - Wordpress Development 上提出了这个问题,得到了 Kieran McClung 的精彩回答。他的答案对我来说非常有效,并在下面复制。

    <form role="search" method="get" id="acf-search" action="<?php site_url(); ?>">
    ...
    
    <input type="text" name="part1" />
    <input type="text" name="part2" />
    <input type="text" name="part3" />
    
    <input type="submit" value="Search" />
    </form>
    
    
    <?php
    // Results page
    
    // Might be worth checking to make sure these exist before doing the query.
    $part_1 = $_GET['part1'];
    $part_2 = $_GET['part2'];
    $part_3 = $_GET['part3'];
    $search_string = $part_1 . '-' . $part_2 . '-' . $part_3;
    
    // Get all product categories
    $categories = get_terms( 'product_cat' );
    $counter = 0;
    
    // Loop through categories
    foreach ( $categories as $category ) {
    
        // Get custom field
        $gc_number = get_field( 'gc_number', $category );
    
        // If field matches search string, echo name
        if ( $gc_number == $search_string ) {
            // Update counter for use later
            $counter++;
            echo $category->name;
        }
    }
    
    // $counter only increases if custom field matches $search_string.
    // If still 0 at this point, no matches were found.
    if ( $counter == 0 ) {
        echo 'Sorry, no results found';
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-03
      • 1970-01-01
      • 2019-04-11
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 2014-08-27
      相关资源
      最近更新 更多