Support

Account

Home Forums General Issues Get Field from Custom Post Typ

Solved

Get Field from Custom Post Typ

  • I currently have

    <?php 
    
    // Variables
    $number_of_events = get_field( 'number_of_events' );
    // print_r($number_of_events);
    $event_categories = get_field('event_categories');
    $event_cat_ids = wp_list_pluck( $event_categories, 'term_id' );
    
    $args = array(
    		'post_type' 		=> 'event',
    		'post_status'		=> 'publish',
    		'posts_per_page'	=> $number_of_events,
    		'order'				=> 'DESC',
    );
    			
    $args['tax_query'] = array(
    	'relation' 		=> 'OR', 
    
    	array(
    		'taxonomy'	=> 'event_category',
    		'field'		=> 'term_id',
    		'terms'		=> $event_cat_ids
    	),			
    		
    );
    
    $query = new WP_Query( $args );
    if( $query->have_posts() ) : ?> 
    	<div class="event-grid">
    		<?php while( $query->have_posts() ): $query->the_post();
    		// $date_number = get_field('date_number');
    		// $date_number = get_field('date_and_time');
    		// $event_title = get_field('event_title');
    		// $event_location = get_field('event_location');
    		// $event_description = get_field('event_description'); 
    		?>
    			<div class="block-box event-box">
    				<div class="event-number-container">
    					<p class="event-number">date<?php //echo $date_number; ?></p>
    				</div>
    				<div class="event-details">
    					<p class="event-date-time"><?php the_field('date_and_time' ?></p>
    					<h4 class="event-title"><?php the_title(); ?></h4>
    					<p class="event-location">event location<?php // echo $event_location; ?></p>
    				</div>
    			</div>
    		<?php endwhile;  wp_reset_postdata();
    endif;
    ?>

    but I want to get the field date_and_time which I attached to object events. How do I take care of this? Tried adding the term, and grabbing it using

    $term = get_queried_object();
    $date_and_time = get_field('date_and_time', $term);

    but no luck yet somehow..

  • Assuming events is a taxonomy, you would need to get the term ID and use that in the get_field code.

    $term = get_queried_object()->term_id;
    $date_and_time = get_field('date_and_time', $term);

    Code untested

  • Events is taxonomy 'post_type' => 'events' but when I add

    $term = get_queried_object()->term_id;
    print_r($term);

    to code here inside the while loop nothing is printed. The new query does already load the events custom post type, but now with fields added I want to load them too.

    <?php 
    
    // Variables
    $number_of_events = get_field( 'number_of_events' );
    // print_r($number_of_events);
    $event_categories = get_field('event_categories');
    $event_cat_ids = wp_list_pluck( $event_categories, 'term_id' );
    
    $args = array(
    		'post_type' 		=> 'event',
    		'post_status'		=> 'publish',
    		'posts_per_page'	=> $number_of_events,
    		'order'				=> 'DESC',
    );
    			
    $args['tax_query'] = array(
    	'relation' 		=> 'OR', 
    
    	array(
    		'taxonomy'	=> 'event_category',
    		'field'		=> 'term_id',
    		'terms'		=> $event_cat_ids
    	),			
    		
    );
    
    $query = new WP_Query( $args );
    if( $query->have_posts() ) : ?> 
    	<div class="event-grid">
    		<?php while( $query->have_posts() ): $query->the_post();
    		$term = get_queried_object()->term_id;
    		print_r($term);
    		// $date_and_time = get_field('date_and_time', $term);
    		// print_r($date_and_time);
    		// $date_number = get_field('date_number');
    		// $date_number = get_field('date_and_time');
    		// $event_title = get_field('event_title');
    		// $event_location = get_field('event_location');
    		// $event_description = get_field('event_description'); 
    		?>
    			<div class="block-box event-box">
    				<div class="event-number-container">
    					<p class="event-number">date<?php //echo $date_number; ?>
    
    				</div>
    				<div class="event-details">
    					<p class="event-date-time"><?php // echo $date_and_time; ?>
    
    					<h4 class="event-title"><?php the_title(); ?></h4>
    					<p class="event-location">event location<?php // echo $event_location; ?>
    
    				</div>
    			</div>
    		<?php endwhile;  wp_reset_postdata();
    endif;
    ?>

    So I used $term = $query->get_queried_object()->term_id; and did get id 146. so making progress here.

    Getting the date_and_time field for posts queried do not load yet though.. we also use more categories for the events cpt of course..

    $term = $query->get_queried_object()->term_id;
    // print_r($term);
    $date_and_time = get_field('date_and_time', $term);
    print_r($date_and_time);

    I guess I may need to focus on the custom post type and not the term here?

  • Here the ACF field group

    [
        {
            "key": "group_6188d209a6110",
            "title": "Events CPT Fields",
            "fields": [
                {
                    "key": "field_6188d22182ad0",
                    "label": "Event Date",
                    "name": "event_date",
                    "type": "date_picker",
                    "instructions": "",
                    "required": 0,
                    "conditional_logic": 0,
                    "wrapper": {
                        "width": "",
                        "class": "",
                        "id": ""
                    },
                    "display_format": "F j, Y",
                    "return_format": "F j, Y",
                    "first_day": 1
                },
                {
                    "key": "field_6188d25582ad1",
                    "label": "Date and Time",
                    "name": "date_and_time",
                    "type": "date_time_picker",
                    "instructions": "",
                    "required": 0,
                    "conditional_logic": 0,
                    "wrapper": {
                        "width": "",
                        "class": "",
                        "id": ""
                    },
                    "display_format": "F j, Y \\@ g:i a",
                    "return_format": "F j, Y \\@ g:i a",
                    "first_day": 1
                },
                {
                    "key": "field_6188d27582ad2",
                    "label": "Event Location",
                    "name": "event_location",
                    "type": "text",
                    "instructions": "Add the event location here",
                    "required": 0,
                    "conditional_logic": 0,
                    "wrapper": {
                        "width": "",
                        "class": "",
                        "id": ""
                    },
                    "default_value": "",
                    "placeholder": "",
                    "prepend": "",
                    "append": "",
                    "maxlength": ""
                }
            ],
            "location": [
                [
                    {
                        "param": "post_type",
                        "operator": "==",
                        "value": "event"
                    }
                ]
            ],
            "menu_order": 0,
            "position": "normal",
            "style": "default",
            "label_placement": "top",
            "instruction_placement": "label",
            "hide_on_screen": "",
            "active": true,
            "description": ""
        }
    ]
  • I see I made it all too hard.

    the_field('date_and_time', get_the_id())

    works just fine. This as it is inside a new query $query = new WP_Query( $args );

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.