Views plugin lets you build your own custom search for any content type. These searches can be based on post content, taxonomies and custom fields.
When you ask for help or report issues, make sure to tell us settings for your custom search.
Viewing 15 topics - 496 through 510 (of 749 total)
Problem: I have a custom field that holds date-like values in the format YYYY-MM. I would like to add a Query Filter to my View based on this custom field, and set it up so that the results are from the current year and the previous two years.
Solution: Use the wpv_filter_query PHP filter to find date fields "like" the 3 most recent years:
// Filter a View by date-like custom field value YYYY-MM, show only results from current year and two previous years
// https://toolset.com/forums/topic/view-which-filters-on-yyyy-mm/
add_filter( 'wpv_filter_query', 'tssupp_yyyy_mm_two_years_ago', 99, 3 );
function tssupp_yyyy_mm_two_years_ago( $query_args, $views_settings, $view_id) {
$view_ids = array( 123 ); // your view ID or IDs
$date_field_slug = 'field-slug'; // your date field slug
// --------- you should not edit below this line ---------------------
if (in_array($view_id, $view_ids)){
$year = date('Y');
$query_args['meta_query'] = array(
'relation' => 'OR',
'year1_clause' => array(
'key' => 'wpcf-'.$date_field_slug,
'compare' => 'LIKE',
'type' => 'STRING',
'value' => $year
),
'year2_clause' => array(
'key' => 'wpcf-'.$date_field_slug,
'compare' => 'LIKE',
'type' => 'STRING',
'value' => ($year-1)
),
'year3_clause' => array(
'key' => 'wpcf-'.$date_field_slug,
'compare' => 'LIKE',
'type' => 'STRING',
'value' => ($year-2)
)
);
}
return $query_args;
}
Problem:
The user has a select field with dynamic option values. The options of the field are generated using Toolset Types filter wpt_field_options. The generated options are the months of the last two years.
Inside a view, the field is not displayed anymore.
Solution:
It turns out that the values that are not displayed are not included in the values generated by the wpt_field_options filter. Toolset returns an empty value.
The solution is to use a custom shortcode that will return the row value from the database.
// The shortcode needs to be added in Toolset->Settings->Custom Code.
add_shortcode('meta', 'meta_fun');
function meta_fun($atts){
global $post;
$atts = shortcode_atts(array(
'field' => NULL,
), $atts);
extract($atts);
if( NULL === $field ) return;
// return "Voila";
return get_post_meta($post->ID, $field, true);
}
// How to use it inside the view
[meta field="wpcf-production-year-month"]