【问题标题】:Filter WordPress posts with custom fields使用自定义字段过滤 WordPress 帖子
【发布时间】:2020-06-01 00:35:30
【问题描述】:

我想了解使用自定义字段过滤特定类别帖子的方法。我想要的结果和表here一模一样 .我已经创建了一个这样的列表,并在表格单元格中插入了三个不同的自定义字段,每个字段都包含许多值。如何使用与上述福布斯杂志列表相同的结构并使用下拉菜单过滤帖子?

【问题讨论】:

  • 请在您的问题中添加一些代码,因为将来链接可能会中断。

标签: php wordpress custom-fields


【解决方案1】:

我认为这个文档很清楚https://codex.wordpress.org/Class_Reference/WP_Meta_Query

但是我不太确定您要的是什么。 您想通过自定义字段过滤 HTML 输出或您的 wordpress 数据库吗?

顺便说一句,如果你想过滤 html 输出,那么使用 jQuery,我曾经使用 data-attribute 过滤表行中的 2000 多个列表;

这里是 jQuery 文档,https://api.jquery.com/category/selectors/

如果您想使用元值过滤和重新排序 wordpress 表 按元值排序

    $args = [
        'meta_key' => '*meta_keyword*',
        'orderby' => 'meta_value',
        'order' => 'ASC'
    ];

meta_keyword 需要根据您的 meta_key 进行更改,您几乎可以按每种类型的值、日期、int 等对它们进行排序

过滤表格

$args['meta_query'] = [
    [
        'key' => 'meta_key',
        'value' => 'filter_value',
        'type' => 'str*',
        'compare' => '=*'
    ]
];
  • '*'取决于值类型和你的逻辑

这里有一些信息 https://wordpress.stackexchange.com/questions/30241/wp-query-order-results-by-meta-value 另一个答案 https://stackoverflow.com/a/24253081/3392555

【讨论】:

    【解决方案2】:

    请在我的博客上找到类似的示例。在这里,我使用自定义字段和类别名称进行过滤,并将它们显示为主页中的一个块。

    http://www.pearlbells.co.uk/filter-posts-custom-fields-wp_query/

    $args = array(
        'category_name' => 'courses',
        'orderby'       => 'menu_order',
        'order'         => 'ASC',
        'meta_query' => array(
            array(
                'key'     => 'front_page',
                'value'   => 'yes',
                'compare' => 'LIKE',
            ))
    
    );
    
    $the_query = new WP_Query( $args );
    

    【讨论】:

      【解决方案3】:

      自定义帖子类型名称 = 软件, 自定义字段名称 = 版本元

      此编码是在 single.php 上完成的。 $version 用于调用 single.php 上的自定义字段值,并通过自定义字段值过滤数据。

      <?php $args = array('post_type' => 'software', 'showposts'=>'4', 'category_name' => '', 'orderby'   => '','meta_query' => array(array('key' => 'version_meta','value' => $version = get_post_meta($post->ID, 'version_meta', true))));
             $loop = new WP_Query( $args );
             while ( $loop->have_posts() ) : $loop->the_post();?>
              <div class="post_desc">
                  <div class="thumb thmbhome3">
                      <?php the_post_thumbnail();?>
                  </div>
                  <div class="title_des edu tthome3">
                      <div class="title edu_titl ttshome3">
                          <h1><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h1>
                      </div>
                  </div>
      
              </div>
      
          <?php endwhile; ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多