Support

Account

Forum Replies Created

  • Yeah, that exact filename is also needed , but that was correct. Perhaps I did not build properly at the time. However, in the end I decided to go full ReactJS/ Blocks and leave out ACF for this block. And that works now. Thanks for the feedback though @tjhole-support !

  • Did you ever solve this? How do you load the ACF meta field for your Gutenberg plugin?

  • Only using

    <php?
    function imwz_rand_posts() { 
     
    $args = array(
        'post_type' => 'molecule',
        'orderby'   => 'rand',
        'posts_per_page' => 1, 
        );
     
    $the_query = new WP_Query( $args );
    $string = ''; 
    if ( $the_query->have_posts() ) {
    
    $string .= '<div>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            $string .= '<h3><a href="'. get_permalink() .'">'. get_the_title() .'</a></h3>';
    				$string .= '<p>' . the_excerpt() . '</p>';
        }
        $string .= '</div>';
        /* Restore original Post Data */
        wp_reset_postdata();
    } else {
     
    $string .= 'no posts found';
    }
     
    return $string; 
    } 
    
    // [imwz-random-posts] shortcode
    add_shortcode('imwz-random-posts','imwz_rand_posts');
    add_filter('widget_text', 'do_shortcode');

    the paragraph part loads above all other data/blocks and not below the title inside the same shortcode block. Any ideas why?

  • Might just need to use a shortcode to be added to a block

    <?php
    function imwz_rand_posts() { 
     
    $args = array(
        'post_type' => 'molecule',
        'orderby'   => 'rand',
        'posts_per_page' => 1, 
        );
     
    $the_query = new WP_Query( $args );
     
    if ( $the_query->have_posts() ) {
     
    $string .= '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            $string .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
        }
        $string .= '</ul>';
        /* Restore original Post Data */
        wp_reset_postdata();
    } else {
     
    $string .= 'no posts found';
    }
     
    return $string; 
    } 
     
    add_shortcode('imwz-random-posts','imwz_rand_posts');
    add_filter('widget_text', 'do_shortcode');

    See https://www.wpbeginner.com/wp-tutorials/how-to-display-random-posts-in-wordpress/

  • As I stated I now have an option to pick a molecule of the day. But I want one at random to load. In ACF we have a motd block with Field Type post object. This seems to create a setup where you can choose an item from the existing ones for the post object or search for one.

    How can we load a post at random using an ACF Block from the editor with a Gutenberg Block?

    current code:

    <?php
    // ACF updated to work with new block
    $motd = get_field('motd');
    var_dump($motd); // NULL
    $molecule_month = get_the_terms($motd->ID , 'molecules_of_the_month' ); 
    var_dump($molecule_month); // warning trying to read property ID on NULL
    
    ?>
    
    <div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
    
    <?php
    // $molecule_month = get_the_terms($motd->ID , 'molecules_of_the_month' );
    // $month = array_pop($molecule_month);
    // $molecule_link = get_term_link( $month->term_id, 'molecules_of_the_month' );
    
    // Set the post type here, and sort them randomly
    // molecule of the month presented as random molecule of the day.
    $args = array(
        'post_type' => 'molecule',
        'posts_per_page'=> 1, 
    		'post_category' => 'molecule_of_the_month',
        'order_by' => 'rand',
    );
    // Initiate a custom query
    	
    $cpt_query = new WP_Query($args);
    	
    // If the query has any post, start the loop
    if($cpt_query->have_posts()){
        while($cpt_query->have_posts()){
            // Output a link and a thumbnail of the post
            $cpt_query->the_post(); ?>
            <div class="random-post">
    					<div class="motd-img">
    						<a href="<?php echo $molecule_link ?>">
    								<?php echo get_the_post_thumbnail( 'full');?>"/>
    						</a>
    					</div>
    					<div class="motd-content">
    						<h2 class="is-style-highlight">Molecule of the day</h2>
    						<h3 class="motd-name">
    							<a href="<?php // echo $molecule_link ?>">
    							<?php // echo esc_html( $title ); ?><?php // if($month){ ?> - <?php //echo $month->name; } ?>
    							</a>
    						</h3>
    						<a href="<?php the_permalink();?>"><?php the_title();?></a>
    					</div>
            </div>
    				<?php
        }
    } ?>
  • Needed to add another location for ACF Block. Loads now. Just still need to select post from selectbox. Need to have one loaded at random instead. Will check some more.

  • Seems the variable motd does not load anything so second check fails as well.

    <?php
    // new acf block field to be added
    $motd = get_field('motd');
    var_dump($motd); // NULL
    $molecule_month = get_the_terms($motd->ID , 'molecules_of_the_month' ); 
    var_dump($molecule_month); // warning trying to read property ID on NULL

    so results show

    NULL
    Warning: Attempt to read property “ID” on null in /Users/jasper/code/site/wp-content/themes/theme/blocks/random-molecule/block.php on line 24
    bool(false)

    And that gets me back to issue with ACF block. I have the base on just loading a molecule on selection and a new one

    <?php
    acf_register_block_type( array(
      'name'			=> 'molecule-of-the-day',
      'title'			=> __( 'Molecule of the day', 'theme' ),
      'render_template'	=> 'blocks/molecule-of-the-day/block.php',
      'category'		=> 'theme',
      'icon'			=> 'image-filter',
      'mode'			=> 'auto',
      'keywords'		=> array( 'molecule' )
      ));
      acf_register_block_type( array(
      'name'			=> 'random-molecule',
      'title'			=> __( 'Random Molecule', 'theme' ),
      'render_template'	=> 'blocks/random-molecule/block.php',
      'category'		=> 'theme',
      'icon'			=> 'image-filter',
      'mode'			=> 'auto',
      'keywords'		=> array( 'molecule' )
      ));

    In the first block $motd = get_field('motd'); works just fine. In my new one under construction it does not. Why is that? Am I doing something now I should not or that is not possible? Just do not understand this.

  • Had to use

    <?php 
    $args = array( 'hide_empty=0', 
            'taxonomy'      => 'molecules_of_the_month',
            'childless'     => 1, 
            'number'        => 6,
            'orderby'       => 'ID', 
            'order'         => 'ASC'
            ); 
    $the_query = new WP_Term_Query($args);
    foreach($the_query->get_terms() as $term) :
    // foreach( $molecules_of_the_month as $month ) : ?>
        <!-- Post Content goes here -->
        <li class="post-grid-post splide__slide">
            <?php
            $summaryimage  = get_field('summary_preview_image', 'term_' .$term->term_id  ); // field, id and format value

    to make it work. Well, that seems to make most of what I want do. Need to limit description, tweak html to work on mobile. But will work well soon.

  • Seems that I need to use WP_Query to loads arguments

    $args = array( 'hide_empty=0', 
                    'childless'     => 1, 
                    'number'        => 6,
                    'orderby'       => 'ID', 
                    'order'         => 'ASC'
                    ); 

    if I want to use these in get_terms. See https://developer.wordpress.org/reference/classes/wp_term_query/get_terms/#user-contributed-notes

    example:

    <ul>
    <?php 
    $args = array(
        'taxonomy'               => 'category',
        'orderby'                => 'name',
        'order'                  => 'ASC',
        'hide_empty'             => false,
    );
    $the_query = new WP_Term_Query($args);
    foreach($the_query->get_terms() as $term){ 
    ?>
        <li><?php echo $term->name." (".$term->count.")"; ?></li>
    <?php
    }
    ?>
    </ul>

    Will look into that.

  • Wait, echo wp_get_attachment_image( $summaryimage, $size ); does seem to try to load images now like:

    <img width="1191" height="1684" src="https://site.test/wp-content/uploads/2021/10/2021-MOTM-Aug.png" class="attachment-full size-full" alt="" loading="lazy" srcset="https://site.test/wp-content/uploads/2021/10/2021-MOTM-Aug.png 1191w, https://site.test/wp-content/uploads/2021/10/2021-MOTM-Aug-212x300.png 212w, https://site.test/wp-content/uploads/2021/10/2021-MOTM-Aug-724x1024.png 724w, https://site.test/wp-content/uploads/2021/10/2021-MOTM-Aug-768x1086.png 768w, https://site.test/wp-content/uploads/2021/10/2021-MOTM-Aug-1086x1536.png 1086w" sizes="(max-width: 1191px) 100vw, 1191px">

    so I am making progress and thanks to you @jarvis . Thanks a lot so far.

    Now I just need to add proper html, include $args, load part of text or excerpt and then I will be done.

  • Okay, using this code

    <div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
      <div class="slider mobile-slider splide" data-splide='{ "type" : "loop", "perPage": "3", "arrows": 0, "breakpoints": { "99999": { "destroy": 1 }, "767": { "perPage": "1" } } }'>
        <div class="splide__track">
            <ul class="post-grid splide__list">
                <?php 
                if( $molecules_of_the_month = get_terms( array( 'taxonomy' => 'molecules_of_the_month', $args ) ) ) :
                    foreach( $molecules_of_the_month as $month ) : 
                        $summaryimage  = get_field('summary_preview_image', 'term_' .$month->term_id  ); // field, id and format value
                        // $summaryimage = get_field('summary_preview_image', 'term_name_', 'molecules_of_the_month'); // NULL++
                        $size = 'full'; // (thumbnail, medium, large, full or custom size)
                        // var_dump($summaryimage);
                        echo '<a href="' . esc_url( get_term_link( $month ) ) . '" alt="' . esc_attr( sprintf( __( 'View all post filed under %s', 'drughunter' ), $month->name) ) . '">' 
                        . $month->name . '</a>';
                    endforeach;
                endif; ?>
            </ul>
        </div>
      </div>
    </div>

    I have the summaryimage loading ids but not in use yet as I need image url. This so I can load the image, title and short description and link it all.

    I also can still load term links using this loop and an echo so that is fine. But need to work out loading image first.

    Furthermore I need to include the $args I had before. Not sure how with current setup. Doing some trials still.

  • @jarvis I used your code with foreach loops:

    <div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
            <div class="slider mobile-slider splide" data-splide='{ "type" : "loop", "perPage": "3", "arrows": 0, "breakpoints": { "99999": { "destroy": 1 }, "767": { "perPage": "1" } } }'>
                <div class="splide__track">
                    <ul class="post-grid splide__list">
                        <?php 
                        if( $molecules_of_the_month = get_terms( array( 'taxonomy' => 'molecules_of_the_month' ) ) ) :
                            foreach( $molecules_of_the_month as $month ) : 
                                $summaryimage  = get_field('summary_preview_image', 'term_' .$month->term_id  );
                                $size = 'full'; // (thumbnail, medium, large, full or custom size)
                                print_r($summaryimage);
                                // $molecules_of_the_month .= '<a href="' . esc_url( get_term_link( $month ) ) . '" alt="' . esc_attr( sprintf( __( 'View all post filed under %s', 'theme' ), 
                                // $month->name ) ) . '">' . $month->name . '</a><figure class="post-featured-image">' . 
                                // wp_get_attachment_image( $summaryimage, $size ) . '</figure>';
                            endforeach;
                        endif; ?>
                    </ul>
                </div>
            </div>
        </div>

    and I know get 328235043514 or this using var_dump

    NULL int(3282) bool(false) bool(false) bool(false) int(3504) int(3514) bool(false) NULL bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false)

    so I am getting something now. So I guess I needed to load the image in the foreach loop. But I also see you used:

    <?php $summaryimage = get_field('summary_preview_image', 'term_' .$month->term_id );?>

    with term_ .$month->term_id which I am clearly not familiar enough with. Besides understanding this better I also need to load the image url so guess I do not want ID numbers only. How would I do that?

    NB had to post this 3 times to make it stick on the forum

  • @jarvis I used your code with foreach loops:

    <div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
            <div class="slider mobile-slider splide" data-splide='{ "type" : "loop", "perPage": "3", "arrows": 0, "breakpoints": { "99999": { "destroy": 1 }, "767": { "perPage": "1" } } }'>
                <div class="splide__track">
                    <ul class="post-grid splide__list">
                        <?php 
                        if( $molecules_of_the_month = get_terms( array( 'taxonomy' => 'molecules_of_the_month' ) ) ) :
                            foreach( $molecules_of_the_month as $month ) : 
                                $summaryimage  = get_field('summary_preview_image', 'term_' .$month->term_id  );
                                $size = 'full'; // (thumbnail, medium, large, full or custom size)
                                print_r($summaryimage);
                                // $molecules_of_the_month .= '<a href="' . esc_url( get_term_link( $month ) ) . '" alt="' . esc_attr( sprintf( __( 'View all post filed under %s', 'theme' ), 
                                // $month->name ) ) . '">' . $month->name . '</a><figure class="post-featured-image">' . 
                                // wp_get_attachment_image( $summaryimage, $size ) . '</figure>';
                            endforeach;
                        endif; ?>
                    </ul>
                </div>
            </div>
        </div>

    and I know get 328235043514 or this using var_dump

    NULL int(3282) bool(false) bool(false) bool(false) int(3504) int(3514) bool(false) NULL bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false)

    so I am getting something now. So I guess I needed to load the image in the foreach loop. But I also see you used:

    <?php $summaryimage = get_field('summary_preview_image', 'term_' .$month->term_id );?>

    with term_ .$month->term_id which I am clearly not familiar enough with. Besides understanding this better I also need to load the image url so guess I do not want ID numbers only. How would I do that?

  • I tried

    <?php $args = array( 'hide_empty=0', 
                    'childless'     => 1, 
                    'number'        => 6,
                    'orderby'       => 'ID', 
                    'order'         => 'ASC'
                    ); 
    
    $terms = get_terms( 'molecules_of_the_month', $args );
    if ( ! empty( $terms )   ) :
        // $termid = get_queried_object()->term_id;
        // var_dump($termid);
        $summaryimage = get_field('summary_preview_image', 'tax_name_', 'molecules_of_the_month');
        // print_r($summaryimage);
        $size = 'full'; // (thumbnail, medium, large, full or custom size)
        $count = count( $terms );
        $i = 0;
        $term_list = '<p class="molecule-of-the-month">';
        ?>
        <div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
            <div class="slider mobile-slider splide" data-splide='{ "type" : "loop", "perPage": "3", "arrows": 0, "breakpoints": { "99999": { "destroy": 1 }, "767": { "perPage": "1" } } }'>
                <div class="splide__track">
                    <ul class="post-grid splide__list">
                        <?php 
                        if($term_list):
                            foreach ( $terms as $term ) {
                                $i++;
                                $term_list .= '<a href="' . esc_url( get_term_link( $term ) ) . '" alt="' . esc_attr( sprintf( __( 'View all post filed under %s', 'theme' ), 
                                $term->name ) ) . '">' . $term->name . '</a><figure class="post-featured-image">' . 
                                wp_get_attachment_image( $summaryimage, $size )
                                . '</figure>';
                                
                                if ( $count != $i ) {
                                    $term_list .= ' &middot; ';
                                }
                                else {
                                    $term_list .= '</p>';
                                }
                            }
                            echo $term_list;
    
                        endif; ?>
                    </ul>
                </div>
            </div>
        </div>
    <?php endif;?>

    and the var_dump for summary_image is NULL. Do not get it. And the ACF field is there:

    if( function_exists('acf_add_local_field_group') ):
    
    acf_add_local_field_group(array(
    	'key' => 'group_612651d5d04ad',
    	'title' => 'Molecule Archive Fields',
    	'fields' => array(
    		array(
    			'key' => 'field_6126521430c38',
    			'label' => 'Slide Deck',
    			'name' => 'slide_deck',
    			'type' => 'file',
    			'instructions' => '',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'return_format' => 'array',
    			'library' => 'all',
    			'min_size' => '',
    			'max_size' => '',
    			'mime_types' => '',
    		),
    		array(
    			'key' => 'field_6126525730c39',
    			'label' => 'Slide Deck Preview Image',
    			'name' => 'preview_image',
    			'type' => 'image',
    			'instructions' => '',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'return_format' => 'id',
    			'preview_size' => 'medium',
    			'library' => 'all',
    			'min_width' => '',
    			'min_height' => '',
    			'min_size' => '',
    			'max_width' => '',
    			'max_height' => '',
    			'max_size' => '',
    			'mime_types' => '',
    		),
    		array(
    			'key' => 'field_6131b9c9bedc9',
    			'label' => 'Summary',
    			'name' => 'summary',
    			'type' => 'file',
    			'instructions' => '',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'return_format' => 'array',
    			'library' => 'all',
    			'min_size' => '',
    			'max_size' => '',
    			'mime_types' => '',
    		),
    		array(
    			'key' => 'field_6131b9e1bedca',
    			'label' => 'Summary Preview Image',
    			'name' => 'summary_preview_image',
    			'type' => 'image',
    			'instructions' => '',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'return_format' => 'id',
    			'preview_size' => 'medium',
    			'library' => 'all',
    			'min_width' => '',
    			'min_height' => '',
    			'min_size' => '',
    			'max_width' => '',
    			'max_height' => '',
    			'max_size' => '',
    			'mime_types' => '',
    		),
    	),
    	'location' => array(
    		array(
    			array(
    				'param' => 'taxonomy',
    				'operator' => '==',
    				'value' => 'molecules_of_the_month',
    			),
    		),
    		array(
    			array(
    				'param' => 'taxonomy',
    				'operator' => '==',
    				'value' => 'molecules_of_the_year',
    			),
    		),
    	),
    	'menu_order' => 0,
    	'position' => 'normal',
    	'style' => 'default',
    	'label_placement' => 'top',
    	'instruction_placement' => 'label',
    	'hide_on_screen' => '',
    	'active' => true,
    	'description' => '',
    ));
    
    endif;
  • Thanks for the feedback @jarvis . When I Use this updated code:

    $terms = get_terms( 'molecules_of_the_month', $args );
    if ( ! empty( $terms )   ) :
        $termid = get_queried_object()->term_id;
        var_dump($termid);
        $summaryimage = get_field('summary_preview_image', 'tax_name_'.$termid);
        // print_r($summaryimage);
        $size = 'full'; // (thumbnail, medium, large, full or custom size)
        $count = count( $terms );
        $i = 0;
        $term_list = '<p class="molecule-of-the-month">';
        ?>

    does however not work … yet either. The var_dump shows me string(0) “”. So besides me not specifying the taxonomy (month here though acf group with field preview_image is attached to term month and year) something else is still needed I think.

  • Good to hear that. Yes, I did not have any issues so far today. Opened a ticket, added an update and no 504s.

  • Did try

    $terms = get_terms( 'molecules_of_the_month', $args );
    if ( ! empty( $terms )   ) :
        $termid = get_queried_object()->term_id;
        print_r($termid);
        $summaryimage = get_field('summary_preview_image', $termid);
        // print_r($summaryimage);
        $size = 'full'; // (thumbnail, medium, large, full or custom size)
        $count = count( $terms );

    but also do not get the term id . Perhaps I need to do another query on top, not sure yet…

  • I think I cannot call $motd cause it is an ACF block with a post object and another ACF block at that. An existing one where you can select a post. I am working on loading a random one… Might need to create another ACF Block Field Group.

  • @hube2 I got somewhat further:

    <?php
    
    $motd = get_field('motd');
    
    ?>
    
    <div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
    
    <?php
    $molecule_month = get_the_terms($motd->ID , 'molecules_of_the_month' );
    $month = array_pop($molecule_month);
    $molecule_link = get_term_link( $month->term_id, 'molecules_of_the_month' );
    
    // Set the post type here, and sort them randomly
    // molecule of the month presented as random molecule of the day.
    $args = array(
        'post_type' => 'molecule',
        'posts_per_page'=> 1, 
    		'post_category' => 'molecule_of_the_month',
        'order_by' => 'rand',
    );
    // Initiate a custom query
    	
    $cpt_query = new WP_Query($args);
    	
    // If the query has any post, start the loop
    if($cpt_query->have_posts()){
        while($cpt_query->have_posts()){
            // Output a link and a thumbnail of the post
            $cpt_query->the_post(); ?>
            <div class="random-post">
    					<div class="motd-img">
    						<a href="<?php echo $molecule_link ?>">
    								<?php echo get_the_post_thumbnail( $motd->ID, 'full');?>"/>
    						</a>
    					</div>
                <a href="<?php the_permalink();?>"><?php the_title();?></a>
            </div><?php
        }
    } ?>
    
    </div>

    But got this error

    : Uncaught TypeError: array_pop(): Argument #1 ($array) must be of type array, bool given in /Users/jasper/code/site/wp-content/themes/theme/blocks/random-molecule/block.php:30

    Why would ID be null? It refers to $month = array_pop($molecule_month); and the field has been set before that line. And we do have molecules of the day(based on month) and molecules of the month.

  • I guess I can better use a repeater field as well then. One to basically load post slides using array_rand() I do not need a radio select box like in the example.

    I would need to use a loop and load a post at random. A basic repeater field example in the ACF Repeater Field tutorial shows how to load a sub field value from a random row of a Repeater field:

    <?php
    $rows = get_field('repeater_field_name' );
    if( $rows ) {
        $index = array_rand( $rows );
        $rand_row = $rows[ $index ];
        $rand_row_title = $rand_row['title'];
        // Do something...
    }

    This loop is close to what I need, but I would need to “just” load a random post from a taxonomy from the last 3-4 months.

    Perhaps I just need an ACF block, in it do a new WP_Query to load custom post type at random from a date range. Will need to think about this more.

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

  • 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": ""
        }
    ]
  • 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?

Viewing 25 posts - 1 through 25 (of 43 total)