Post Grid Display Custom Field
-
I’m using your super rad Posts Grid block to display a list of custom post types, namely Events from The Events Calendar. It works great except I need the date and time of the events which are held in meta values. Can I somehow display custom fields / meta values with these posts? Thanks!
-
Hello @bozzmedia,
Please use the following filter to display custom field value in the Post Grid block:
function single_post_meta_before( $post_id, $attributes ) { //get_field('custom_field_name',Post_ID); to get the value of ACF custom field $value1 = get_field( 'custom_date', $post_id ); $value2 = get_field( 'custom_time',$post_id ); echo $value1.'<br>'; echo $value2; } add_action( 'uagb_single_post_before_meta_grid', 'single_post_meta_before', 10, 2 );
I hope this helps.
Regards,
Sweta- This reply was modified 4 years, 7 months ago by Team Brainstorm Force.
This does help, thank you Sweta!
How do I target specific instances of Post Grid so that this doesn’t impact every post grid instance? Thanks again!
You’re welcome, @bozzmedia!
Please use the following filter:
function single_post_meta_before( $post_id, $attributes ) { if( 'my-unique-class' == $attributes['className'] ){ //get_field('custom_field_name',Post_ID); $value1 = get_field( 'date', $post_id ); $value2 = get_field( 'custom_time',$post_id ); echo $value1.''; echo $value2; } } add_action( 'uagb_single_post_before_meta_grid', 'single_post_meta_before', 10, 2 );
Please check this screenshot where you can add the unique class in post grid block.
I hope this helps.
Regards,
SwetaHello @bozzmedia
Since it’s been quite some time since I heard from you, I assume that this is resolved and therefore, I am marking it resolved for now.
Please feel free to let us know if you need any further help.
Regards,
RajkiranThank you for all the insight. I have crafted a custom query based on all of this and it seems to be working, though I get one debug error:
Notice: Undefined index: className in /home/redact/public_html/redact/wp-content/themes/redact/functions.php on line 72
*Line 72 is the second function where className is referenced for the second time
Here is my code:
// Post Grid Events function single_post_meta_before( $post_id, $attributes ) { if( 'events-grid' == $attributes['className'] ){ //get_field('custom_field_name',Post_ID); // $value1 = get_field( 'date', $post_id ); $value1 = do_shortcode('[tribe_formatted_event_date id="' . $post_id . '" format="F j, Y"]'); // $value2 = get_field( '_EventStartDate',$post_id ); echo $value1.''; // echo $value2; } } add_action( 'uagb_single_post_before_meta_grid', 'single_post_meta_before', 10, 2 ); function filter_post_query( $query_args, $attributes) { if ( 'events-grid' == $attributes['className'] ) { $bozzargs = array( // WordPress has all the results, now, return only the events after today's date array( 'key' => '_EventStartDate', // Check the event start date 'value' => date("Y-m-d"), // Set today's date (note the similar format) 'compare' => '>=', // Return the ones greater than or equal to today's date 'type' => 'DATE' // Let WordPress know we're working with date ) ); $query_args['orderby'] = '_EventStartDate'; $query_args['order'] = 'ASC'; $query_args['meta_query'] = $bozzargs; } return $query_args; } add_filter( 'uagb_post_query_args_grid', 'filter_post_query', 10, 2 );
Hello @bozzmedia,
The Undefined index Notice occurs when no Class name is added to any Post Grid Block in the Advanced option.
As we only add Class Name to the block we want to filter, so this Notice is shown for the other blocks to which we have not added the Class Name.
To avoid this Notice we can check if the $atributes[‘className’] is set or not and then perform further check.
For example:
function filter_post_query( $query_args, $attributes) { if ( isset( $attributes['className'] ) && 'events-grid' == $attributes['className'] ) { $bozzargs = array( // WordPress has all the results, now, return only the events after today's date array( 'key' => '_EventStartDate', // Check the event start date 'value' => date("Y-m-d"), // Set today's date (note the similar format) 'compare' => '>=', // Return the ones greater than or equal to today's date 'type' => 'DATE' // Let WordPress know we're working with date ) ); $query_args['orderby'] = '_EventStartDate'; $query_args['order'] = 'ASC'; $query_args['meta_query'] = $bozzargs; } return $query_args; } add_filter( 'uagb_post_query_args_grid', 'filter_post_query', 10, 2 );
As you can see I have checked the isset( $attributes[‘className’] ) in the If statement, which will prevent the Notice to appear.
Hope it helps, let me know how it goes.
Regards,
RajkiranBrilliant, thank you very much for the excellent support!
I am glad that we could help you out, @bozzmedia! ??
Do let us know if there’s anything else we can help you with.
- The topic ‘Post Grid Display Custom Field’ is closed to new replies.