Support

Account

Forum Replies Created

  • Yes, “flexible_content” is the name of the flex field I’m wanting to load when a new post or page is created.

    My code solves the issue, I was posting in case anyone else was trying to do the same, but there is no option to mark it as a solution. 🙂

  • @aaronrobb
    Sorry, I don’t recall what project this issue came up in, so I don’t have a solution at hand for it.

  • getting all of them is straight forward, except the blog posts that need to pull from cat 304.. I can pull all the CPTs together, OR the blog posts.. I just don’t know how to pull a query that gets all the CPTs and then adds the cat arg for the posts.

    If I can get them all together, sorted by date.. that would be perfect.

  • I’d still like to sort out how to query all the posts for each of these types, merge them together and sort by date…

    This is what I’ve wrote up so far:

    
    <?php
    $my_post_types = array( 'auctions', 'liquidations', 'inventory', 'post' );
    
      $recent_auctions = get_posts( array(
        'post_type' 			=> 'auction',
        'posts_per_page' 	=> get_sub_field('items_per_row'),
        'orderby' 				=> 'date',
        'order' 					=> 'DESC',
        'post_status'     => 'publish',
      ));
      $recent_liquidations = get_posts( array(
        'post_type' 			=> 'liquidation',
        'posts_per_page' 	=> get_sub_field('items_per_row'),
        'orderby' 				=> 'date',
        'order' 					=> 'DESC',
        'post_status'     => 'publish',
      ));
      $recent_inventory = get_posts( array(
        'post_type' 			=> 'inventory',
        'posts_per_page' 	=> get_sub_field('items_per_row'),
        'orderby' 				=> 'date',
        'order' 					=> 'DESC',
        'post_status'     => 'publish',
      ));
      $recent_studies = get_posts( array(
        'post_type' 			=> 'post',
        'cat'   					=> 304 ,
        'posts_per_page' 	=> get_sub_field('items_per_row'),
        'orderby' 				=> 'date',
        'order' 					=> 'DESC',
        'post_status'     => 'publish',
      ));
    
    $mergedposts = array_merge( $recent_auctions, $recent_liquidations, $recent_inventory, $recent_studies ); //combine queries
    $postids = array();
    foreach( $mergedposts as $item ) {
      $postids[]=$item->ID; //create a new query only of the post ids
    }
    $uniqueposts = array_unique($postids); //remove duplicate post ids
    $posts = get_posts(array(
      //new query of only the unique post ids on the merged queries from above
      'post__in' => $uniqueposts,
      'post_type' => $my_post_types,
      'post_status' => 'publish',
    ));
    
    foreach( $posts as $post ) : setup_postdata($post); ?>
        <div class="row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4">
          <?php $type = get_sub_field('content_type'); ?>
    
          <?php if($type == 'auctions'): ?>
              <?php get_template_part( 'template-parts/card', 'auction' ); ?>
    
          <?php elseif($type == 'liquidation'): ?>
              <?php get_template_part( 'template-parts/card', 'liquidation' ); ?>
    
          <?php elseif($type == 'inventory'): ?>
          <?php get_template_part( 'template-parts/card', 'inventory' ); ?>
          
          <?php elseif($type == 'studies'): ?>
          <?php get_template_part( 'template-parts/card', 'studies' ); ?>
    
          <?php endif; // end if ?>
        </div><!-- row -->
    <?php endforeach; ?>
    <?php wp_reset_postdata(); ?>
    

    Any advice on merging queries like this??

  • THAT did it! I had tried with it NOT commented, and the page wouldn’t load.. so I commented it out.
    Now, works.. must have been the other errors that thru the wp_reset for a loop – no pun intended.

    Thank you. for all your help!

  • If I comment the pre_get_posts out.. there is no change

  • Yes, I have some pre_get_post filters in place for the CPT pages.

    function my_pre_get_posts( $query ) {
    	global $wpdb;
    
    	// We do not want unintended consequences.
    	if ( is_admin() || !$query->is_main_query() ) {
    		return $query;
    	}
    
    	// Adding all CPTUI post types to the archives
    	if ( is_category() && empty( $query->query_vars['suppress_filters']) ) {
    		// Replace these slugs with the post types you want to include.
    		$cptui_post_types = array( 'inventory', 'auction', 'liquidation' );
    		$query->set(
    	  		'post_type',
    			array_merge(
    				array( 'post' ),
    				$cptui_post_types
    			)
    		);
    	}
    
    	// Include CPTUI post types in Search Results
    	if ( $query->is_search ) {
    
          $query->set( 'post_type', array( 'page','post', 'inventory', 'auction', 'liquidation' ) );
    
      }	elseif(	isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'auction' ) {
    
    			$query->set('numberposts', 12);
    
    	} elseif( $query->query_vars['category_name'] == 'packaging-equipment' || $query->query_vars['category_name'] == 'processing-equipment' || isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'inventory') {
    
    			// remove the archived items from view
    			$query->set( 'cat', '-307' );
    
    			// set the orderby so NEWEST and AVAILABLE are first.
          $query->set( 'meta_query', array(
                array( 'key' => 'statecode' )
            ));
        // another way
        // $query->set('meta_key', 'statecode');
    
    			$query->set( 'orderby', array('meta_value' => 'ASC','date' => 'DESC') );
    
    		// IF item_oem querystring is present, filter posts by value
    		if( isset($_GET['item_oem'] ) && !empty($_GET['item_oem'] )) {
    
    		  // Making the item safe for use.  So there is now HTML embedded.
    		  $oem = htmlspecialchars($_GET['item_oem'], ENT_QUOTES);
    
    			$query->set('post_type', 'inventory');
    			$query->set('meta_key', 'item_oem');
    			$query->set('meta_value', $oem);
    			$query->set('meta_compare', '=' );
    			$query->set( 'orderby', 'meta_value' );
    			$query->set( 'order', 'ASC' );
    
    		// IF show_per_page query string is set, update numberposts to param value to allow visitor to show specific number of posts per page
    	} elseif( isset( $_GET['show_per_page'] ) && !empty($_GET['show_per_page']) ) {
    
    		$show_per_page = htmlspecialchars($_GET['show_per_page'], ENT_QUOTES);
    		$query->set('posts_per_page', $show_per_page);
    
    	} elseif( isset( $_GET['newly_acquired'] ) && $_GET['newly_acquired'] == 'true' && is_post_type_archive('inventory')) {
    
    			$query->set('post_type', 'inventory');
    
    			// let's only show items from the last 30 days.. when the button is clicked.
    			$date_query = array(
     				array(
     					'after' => '-30 days',
     					'column'=> 'post_date',
     					),
    				);
    
    			// set the date for the query
    			$query->set( 'date_query', $date_query 	);
    
    		} else {
    
    			$query->set( 'posts_per_page', 12 );
    
    		}
    	}
    
    	// set loop for articles, employment and case study archives
    }
    add_filter( 'pre_get_posts', 'my_pre_get_posts', 99999);
    
  • If I just run this:

      if (have_rows('row_order_and_qty')) {
        while(have_rows('row_order_and_qty')){ the_row();
    
          $post_type = get_sub_field('content_type'); echo $post_type;
    
          $posts_per_page = get_sub_field('items_per_row'); echo $posts_per_page;
    

    to show the ACF values.. I get the correct values:

    inventory4 auction8 liquidation4 studies8

    Something happens when this code is used in a loop or something. I’ve never seen this before, it usually just works LOL

  • Just prints the first period.. one showing.

    here is the code so far

    <?php
    
      if (have_rows('row_order_and_qty')) {
        while(have_rows('row_order_and_qty')){ the_row();
    
          $post_type = get_sub_field('content_type'); echo $post_type;
    
          $posts_per_page = get_sub_field('items_per_row'); echo $posts_per_page;
    
          $row_query = new WP_Query(array(
            'post_type' => $post_type,
            'post_status' => 'publish',
            'posts_per_page' => $posts_per_page,
            'orderby' => 'date',
            'order' => 'DESC'
          ));
    
          if ($row_query->have_posts()) {
            switch($post_type) {
              case 'auction':
                $icon = '<i class="fas fa-gavel"></i>';
                $heading = 'Auctions';
                $class = 'row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4';
                $link_url = '/auctions';
                break;
              case 'liquidation':
                $icon = '<i class="fas fa-file-invoice-dollar"></i>';
                $heading = 'Liquidations';
                $class = 'row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4';
                $link_url = '/past-auctions';
                break;
              case 'inventory':
                $icon = '<i class="fas fa-award">';
                $heading = 'Featured Inventory';
                $class = 'row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4';
                $link_url = '/inventory';
                break;
              case 'studies':
                $icon = '<i class="fas fa-file-invoice"></i>';
                $heading = 'Case Studies';
                $class = 'row row-cols-1 row-cols-md-2 row-cols-lg-3';
                $link_url = '/studies';
                break;
            } // end switch
            ?>
              <h2 class="section-title mt-5"><?php echo $icon; ?> <?php echo $heading; ?> <a href="<?php echo $link_url; ?>" class="font-weight-normal text-right badge badge-primary">View All</a></h2>
              <div class="<?php echo $class; ?>">
                <?php
                  while ($row_query->have_posts()) {
                    $row_query->the_post(); ?>
                      <div class="col mb-4">
                        <?php // get_template_part('template-parts/card', $type); ?>
                      </div>
                    <?php
                  } // end while have posts
                  //wp_reset_postdata();
                ?>
              </div>
            <?php
          } // end if have posts
        } // end while have rows
      } // end if have rows
    ?>
    

    Image of results so far

  • Can’t believe I missed this.. it was missing row(); after the while statement.

    But, still only prints the first post in the loop.

    <?php
    
      if (have_rows('row_order_and_qty')) {
        while(have_rows('row_order_and_qty')){ the_row();
    
          $post_type = get_sub_field('content_type'); echo $post_type;
    
          $posts_per_page = get_sub_field('items_per_row'); echo $posts_per_page;
    
          $row_query = new WP_Query(array(
            'post_type' => $post_type,
            'post_status' => 'publish',
            'posts_per_page' => $posts_per_page,
            'orderby' => 'date',
            'order' => 'DESC'
          ));
    
          if ($row_query->have_posts()) {
            switch($post_type) {
              case 'auction':
                $icon = '<i class="fas fa-gavel"></i>';
                $heading = 'Auctions';
                $class = 'row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4';
                $link_url = '/auctions';
                break;
              case 'liquidation':
                $icon = '<i class="fas fa-file-invoice-dollar"></i>';
                $heading = 'Liquidations';
                $class = 'row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4';
                $link_url = '/past-auctions';
                break;
              case 'inventory':
                $icon = '<i class="fas fa-award">';
                $heading = 'Featured Inventory';
                $class = 'row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4';
                $link_url = '/inventory';
                break;
              case 'studies':
                $icon = '<i class="fas fa-file-invoice"></i>';
                $heading = 'Case Studies';
                $class = 'row row-cols-1 row-cols-md-2 row-cols-lg-3';
                $link_url = '/studies';
                break;
            } // end switch
            ?>
              <h2 class="section-title mt-5"><?php echo $icon; ?> <?php echo $heading; ?> <a href="<?php echo $link_url; ?>" class="font-weight-normal text-right badge badge-primary">View All</a></h2>
              <div class="<?php echo $class; ?>">
                <?php
                  while ($row_query->have_posts()) {
                    $row_query->the_post(); ?>
                      <div class="col mb-4">
                        <?php // get_template_part('template-parts/card', $type); ?>
                      </div>
                    <?php
                  } // end while have posts
                  //wp_reset_postdata();
                ?>
              </div>
            <?php
          } // end if have posts
        } // end while have rows
      } // end if have rows
    ?>
    

    Link to Results of current code

  • Yeah, I made the switch the $post_type var… but, doesn’t work.
    I even added some echos so see if I could get the values to print

          $post_type = get_sub_field('content_type'); echo $post_type;
    
          $posts_per_page = get_sub_field('items_per_row');echo $posts_per_page;
    

    But nothing displays (I also tried after removing the echos below, to get rid of the errors but no values from the ACF fields show). I also added a default value to the switch, just in case, and it doesn’t print either
    .

  • I tried your code, very clean by the way! The $type var needed to be updated to $post_type… but other than that the vars in the switch case come back as undefined

    Notice: Undefined variable: icon, etc…

    Thoughts?? It’s like the switch case is having no effect on setting those vars

  • Thank you for the time to look over the code.

    My initial goal was to show all the CPTs and Posts, on one page, sorted by date.

    But, I’ve never updated $args within a loop… so, heading this direction of showing rows of each type.

    My original logic for this was:

        $my_post_types = array( 'auctions', 'liquidations', 'inventory',  'post' );
    
        // set args for ALL posts
        $args = array(
            'post_type'     => $my_post_types,
            'orderby'       => 'date',
            'order'         => 'DESC',
            'post_status'   => 'publish',
        );
        
        Now we loop through posts (while loop)
    
        IF 'post_type == auctions' 
            change number_posts to $args from get_sub_field('items_per_row')
            get_template_part( 'template-parts/card', 'auction' );  
    
        IF 'post_type == liquidations' 
            change number_posts to $args from get_sub_field('items_per_row')
            get_template_part( 'template-parts/card', 'liquidations' );  
    
        IF 'post_type == inventory' 
            change number_posts to $args from get_sub_field('items_per_row')
            get_template_part( 'template-parts/card', 'inventory' );  
    
        IF 'post_type == post' 
            add 'cat => 304' to $args 
            change number_posts to $args from get_sub_field('items_per_row')
            and get_template_part( 'template-parts/card', 'studies' );  
    

    Since it was to be sorted by date, I wouldn’t need to setup the fields like I have now (so they can re-order them). I just need to allow them to choose the qty of posts shown for each type, let the loop sort by date.

    Any thoughts on this approach?

  • Yes. only a single row of the initial repeater will show, if I REMOVE the flex field from the case-studies template file.

    With the Flex field in place, the page spools till it times out.

    If I remove the flex field and use get_posts instead of wp query, all of them show.. !

  • I’ve checked the template files, one has a loop for a flexible content field. So, for case studies (basic blog post/category, not CPT) I have flexible content fields. I loop through just one of them (in the template include) and break after the first ‘paragraph’ content field… just to get the content for that case study.

    If I remove that flexible content loop from the include.. the page LOADS but with only the first row of the repeater! IF I use get_posts instead of WP query, it loads all of the rows…

    Here is the flexible content loop from the case-studies.php file:

    			<?php if (have_rows('flexible_content_types')): ?>
    				<?php while (have_rows('flexible_content_types')): the_row(); ?>
    					<?php if (get_row_layout() == 'paragraph_content'): ?>
    						<?php // get the FIRST paragraph content only, truncate it ?>
    						<p class="mb-3"><?php echo truncate(get_sub_field('content'), 180); ?></p>
    						<?php break; ?>
    					<?php endif; ?>
    				<?php endwhile; ?>
    			<?php  endif; ?>
    
  • In the admin,
    I have these four rows of fields (post type / qty per row):

    inventory 4
    liquidation 4
    auction 8
    studies 8

    As a test, if I run this code:

    <?php switch ($type):
    	case 'auction': ?>
    		<?php the_sub_field('content_type'); ?>
    		<?php the_sub_field('items_per_row'); //echo $wp_query->request;?>
    	<?php break; case 'liquidation': ?>
    		<?php the_sub_field('content_type'); ?>
    		<?php the_sub_field('items_per_row'); //echo $wp_query->request;?>
    	<?php break; case 'inventory': ?>
    		<?php the_sub_field('content_type'); ?>
    		<?php the_sub_field('items_per_row'); //echo $wp_query->request;?>
    	<?php break; case 'studies': ?>
    		<?php the_sub_field('content_type'); ?>
    		<?php the_sub_field('items_per_row'); //echo $wp_query->request;?>
    	<?php break; default: ?>
    		<?php echo 'No posts to show'; ?>
    <?php break; endswitch; ?>
    

    I get:

    inventory 4 liquidation 4 auction 8 studies 8
    

    Which is correct. It’s when I replace the sub_fields in those switch cases, or the if/else statements that the loop stops on the first one.. and I get:

    inventory 4
    
  • Clarification… if i run the queries outside the if repeater, I get ALL the correct post types showing.

    IF I run it IN the repeater, only the first one shows.

    I’ve also tested this with an switch statement,.. and only get one of the post types… not all of them.

  • Found the solution:

    function acf_load_icon_field_choices( $field ) {
        // reset choices
        $field['choices'] = array();
        // get the textarea value from options page without any formatting
        $choices = get_field('icon_values', 'option', false);
        // explode the value so that each line is a new array piece
        $choices = explode("\n", $choices);
        // remove any unwanted white space
        $choices = array_map('trim', $choices);
        // loop through array and add to field 'choices'
        if( is_array($choices) ) {
            foreach( $choices as $choice ) {
                $exploded = explode(' : ', $choice);
                $field['choices'][ $exploded[0] ] = $exploded[1];
              //  $field['choices'][ $choice ] = $choice;
            }
        }
        // return the field
        return $field;
    }
    add_filter('acf/load_field/name=left_box_icon', 'acf_load_icon_field_choices');
    
  • When using this with a repeater that is IN a Flexible Content field.. I get this error:
    Warning: count(): Parameter must be an array or an object that implements

  • Wish this would work for Options sub pages (acf_add_options_sub_page).. but, currently, the post_id param is only available to the acf_add_options_page function.
    This would make for a clean admin menu and an easy way to target specific option page fields within specific template files (especially when dealing with archive pages or plugin template pages -eg event calendars etc.)

  • I’m running into a similar situation.
    I have a flexible content field (tabbed_content repeater) that allows the user to add a Tab Content section, and add tabs via a repeater.
    When they add a new post I’d like to be able to ‘auto populate’ that page with a tabb_content element, but still allow them to add/remove tabs (which is a repeater as mentioned).

    Is there any way to programmatically add this flexible content item of tabbed_content, with a predefined set of tabs..?

  • This is a solution for using GROUPS within a REPEATER. These are located in a Flexible Content field of Comparison – omit this IF statement if you’re not using within a Flexible Content field.

    Specifically, I have a REPEATER named ‘comparison_rows’ containing 4 Groups
    (The groups are named column_1, column_2, column_3 and column_4)

    This is so the user can create a comparison table, with the fields for each cell grouped together. Each cell contains an image, text and specific CSS class (which I use ‘type’ to change)

    Column_1 has these fields: text1, type1 and image1
    Column_2 has these fields: text2, type2 and image2
    and so on…

    The fields in each group are:
    text (text fields, go figure)
    type (select fields, with a list of classes, used to change classes on the elements)
    image (image fields returning the URL)

    Here’s is what worked:

    
    <?php elseif (get_row_layout() == 'comparison'): ?>
    	<?php if(have_rows('comparison_rows')): ?>
    		<?php while(have_rows('comparison_rows')): the_row(); ?>
    			<?php
    			$column_1 = get_sub_field('column_1'); // name of group 1
    			$column_2 = get_sub_field('column_2'); // name of group 2.. and so on			$column_3 = get_sub_field('column_3');
    			$column_4 = get_sub_field('column_4');
    			?>						
    				<div class="<?php echo $column_1['type1']; ?>">
    					<?php if ($column_1['image1'] ): ?>
    						<img src="<?php echo $column_1['image1']; ?>"  class="comparison-img"  />
    					<?php endif; ?>
    					<p><?php echo $column_1['text1']; ?></p>
    				</div>
    				
    				<div class="<?php echo $column_2['type2']; ?>">
    					<?php if ($column_2['image2'] ): ?>
    						<img src="<?php echo $column_2['image2']; ?>"  class="comparison-img" />
    					<?php endif; ?>
    					<p><?php echo $column_2['text2']; ?></p>
    				</div>
    				
    				<div class="<?php echo $column_3['type3']; ?>">
    					<?php if ($column_3['image3'] ): ?>
    						<img src="<?php echo $column_3['image3']; ?>"  class="comparison-img" />
    					<?php endif; ?>								
    					<p><?php echo $column_3['text3']; ?></p>
    				</div>
    				
    				<div class="<?php echo $column_4['type4']; ?>">
    					<?php if ($column_4['image4'] ): ?>
    						<img src="<?php echo $column_4['image4']; ?>"  class="comparison-img" />
    					<?php endif; ?>								
    					<p><?php echo $column_4['text4']; ?></p>
    				</div>
    					
    			<?php endwhile; ?> 
    	<?php endif; ?>
    <?php endif; ?>
    
    

    Maybe this will help someone down the road.

  • I’ve tried looping through the groups with:

    
    	<?php if( have_rows('column1') ): ?>
    	    <?php while( have_rows('column1') ): the_row(); 
    	        the_sub_field('type');
    	        the_sub_field('image')['url'];
    	        the_sub_field('text');
    	        ?>
    	    <?php endwhile; ?>
    	<?php endif; ?>
    

    and

    
    	<?php $column1 = get_sub_field('column1'); ?>
    		
    	<?php if( $column1 ): ?>
    		<?php echo $column1['text']; ?>
    
    		// let's see if the value is in the sub_field()			
    	        <?php the_sub_field('type'); ?>
    	        <?php the_sub_field('image')['url']; ?>
    	        <?php the_sub_field('text'); ?>
    	<?php endif; ?>
    

    🙁

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