过滤WordPress帖子与自定义字段


Filter WordPress posts with custom fields

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

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

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

顺便说一句,如果你想过滤html输出,然后使用jQuery,我曾经过滤超过2000个列表在表行使用data-attribute;

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

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

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

meta_keyword需要根据您的meta_key进行更改,您可以根据每种类型的值,Date, 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

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

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 );

自定义邮件类型name = Software,自定义字段名= version meta

这个代码是在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; ?>