I would like to find out what is the way to filter posts of a specific category using custom fields. The result I want is exactly the same as the table here . I have created a list like that and inside the table cells I have inserted three different custom fields each one containing a number of values. How can I use the same structure as above the list of forbes magazine and filter posts using drop down menus?
3 Answers
I think this documentation is pretty clear https://codex.wordpress.org/Class_Reference/WP_Meta_Query.
However i am not quite sure what you are asking for. do you want to filter the HTML output or your wordpress database by custom field??
btw, if you want to filter the html output, then use jQuery, i used to filter over 2000 List in Table row using data-attribute;
Here is the jQuery Documentation, https://api.jquery.com/category/selectors/
if you want to filter and reorder the wordpress table using the meta value To order the by meta value
$args = [
'meta_key' => '*meta_keyword*',
'orderby' => 'meta_value',
'order' => 'ASC'
];
meta_keyword need to change based your on meta_key, you can order them pretty much by every type of value, Date, int etc
to filter the table
$args['meta_query'] = [
[
'key' => 'meta_key',
'value' => 'filter_value',
'type' => 'str*',
'compare' => '=*'
]
];
- '*' Depend on value type and your logic
Here some information https://wordpress.stackexchange.com/questions/30241/wp-query-order-results-by-meta-value Another answer https://stackoverflow.com/a/24253081/3392555
Please find the similar example on my blog. Here I am filtering using the custom fields and category name and displaying those as a block in the home page.
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 );
Custom Post type name = Software, Custom field name = version meta
This coding is done on single.php. $version is used for call the custom field value on single.php and filter the data by custom field value.
<?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; ?>