Support

Account

Forum Replies Created

  • You’re my hero 🙂 Thank you.

    Final code:

    add_action( 'pre_get_posts', 'book_custom_orderby' );
    function book_custom_orderby( $query ) {
      if ( ! is_admin() )
        return;
      if (empty($query->query_vars['post_type']) || $query->query_vars['post_type'] != 'book') {
      return;
      }
      if (empty($_GET['orderby'])) {
        $query->set( 'meta_key', 'order-no' );
        $query->set( 'orderby', 'meta_value_num' );
    	$query->set( 'order', 'ASC' );
      }
    }
  • Just noticed that the code change fixes my sorting issue on the CPT page, but makes the pages and posts lists empty 🙁

  • Yeap, you’re right. This does the trick. Thanks!

    Just for future reference, here is my working code:

    add_action( 'pre_get_posts', 'book_custom_orderby' );
    function book_custom_orderby( $query ) {
      if ( ! is_admin() )
        return;
      if (empty($_GET['orderby'])) {
        $query->set( 'meta_key', 'order-no' );
        $query->set( 'orderby', 'meta_value_num' );
    	$query->set( 'order', 'ASC' );
      }
    }
  • The case is that manual sorting works – the column is sortable and when I click the column the order changes and is sored the way I want. However what I want to achieve is automatic sorting, i.e. when I open the post page all posts are sorted by this field/column right away, without clicking the column name.

  • This gives me, as expected, the date as the query input. However, I thought that the rest of the snippet provides different query parameters, doesn’t it?

  • @hube2 – I believe I answered that in my post.
    Anyway, the last thing doesn’t work – sorting by the ACF field. My fields are still sorted by default by date.

  • Ok, I managed to deal with the code to output the repeater and subfields on the user’s profile page where the repeater fields are placed on the backend profile page of that user and make it in a nice table:

    function acf_repeater() {
    
       $user_id = get_current_user_id();
       ob_start(); ?>
       <?php if( have_rows('repeater-name',"user_{$user_id}" ) ): ?>
    
    	<table>
    	   <tr>
    	   	<td>Header1:</td><td">Header2:</td><td>Header3:</td><td>Header4:</td>
    		</tr>
    
    	<?php while ( have_rows('repeater-name', "user_{$user_id}" ) ) : the_row(); 
    
    		// vars
    		$subfieldname1 = get_sub_field('sub-field-name-1');
    		$subfieldname2 = get_sub_field('sub-field-name-2');
    		$subfieldname3 = get_sub_field('sub-field-name-3');
    		$subfieldname4 = get_sub_field('sub-field-name-3');
    
    	?>
    	   <tr>
    	      <td><?php echo $subfieldname1; ?></td><td><?php echo $subfieldname2; ?></td><td><?php echo $subfieldname3; ?></td><td><?php echo $subfieldname4; ?></td>
    	   </tr>
    		
    
    	<?php endwhile; ?>
    
    	</table>
    
    <?php endif; ?>
    <?php $output = ob_get_clean();
        return $output;
    }
    add_shortcode('acf_repeater_shortcode', 'acf_repeater');

    I hope this will be usefull to someone…

  • Hi @lsell ,
    I have a similar thing to achieve. I used the @pawciak code to show a column in the dashboard’s users page that consists of an ACF text field:

    add_filter( 'manage_users_columns', 'column_register_acf_id' );
    
    add_filter( 'manage_users_custom_column', 'column_display_acf_id', 10, 3 );
    
    function column_register_acf_id( $columns )
    {
    
        $columns['id_col'] = 'ID';
        return $columns;
    }
    
    function column_display_acf_id( $value, $column_name, $user_id )
    {
      $user_info = get_user_meta( $user_id, 'czlonek_id', true );
      if($column_name == 'id_col') return $user_info;
      return $value;
    
    }

    czlonek_id is my ACf field name (location: user form > equal > add/edit). However this doesn’t work – the column shows up, but the value does not.
    Any advice? Thx.

  • @devfx – is there a way to modify this script of yours to include the current user instead of the current post so that in case of a repeater field placed on the backend user profile page you can output that data via a shortcode on the frontend.

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