Support

Account

Home Forums Search Search Results for 'q'

Search Results for 'q'

reply

  • If it is after the regular loop and not inside “The Loop” then get_the_ID() is likely not returning the correct ID if it is returning an ID at all. You should check this

    
    // before query
    var_dump(get_the_ID());
    
  • That depends on the type of relationship field. A relationship field that only allows 1 selections is stored as the post ID of that selection and then you would query for posts there the value “=” that post ID. A relationship field that allows multiple values is stored as a serialized array and requires the “” around the post ID.

    More question:

    Are you using guberbug blocks? Is the relationship field a block?

    If not the above, is the relationship field a sub field of another field; repeater, group, flexible content?

  • It would need more work to accomplish.

    The easiest solution would be to

    1. Create an ACF field that this field can be conditional on, for example a radio field that has all the possible values in it.
    2. Set your conditional fields
    3. Create an acf/prepeare_field filter that sets the value of this radio field base
    4. Use custom CSS to hide this field on the edit page so that users cannot change the values.

    If the page property is something that can be edited, for example based on the a taxonomy term, then you would need to add custom JavaScript that detects that WP field being changed and then updates the value of the field that is hidden by CSS and then

    
    $('your field selector').trigger('change');
    

    More complicated, but possible would be to build your own conditional logic script that detects this the change and then manipulates the class of the field you want to be conditional, this requires adding or removing the class ‘hidden-by-conditional-logic’ to the field container.

    Overall, the easiest way is to have multiple field groups that have locations setting set appropriately and this is what I shoot for unless there is some requirement that dictates otherwise.

  • Hi John,
    Thanks for taking the time.
    I removed it because of this (last comment)
    https://stackoverflow.com/questions/42011047/wordpress-query-by-acf-relationship-field

    It’s still not working. But it should right ?
    I’m wondering if I did something backward or totally miss something here.

  • try

    
    $related = get_posts(array( 'post_type' => 'post', 'meta_query' => array( array( 'key' => 'related_expertises', 'value' => '"'.get_the_ID().'"' , 'compare' => 'LIKE' )) ));
    

    Note that I am enclosing the post ID value in “”

  • The first thing you need to figure out is how you can sort the posts in this module that you are using by a custom field. This is not something that I can help you with. This would be specific to that module and not ACF. Perhaps they supply some type of filter hook that let’s you alter the post query? I don’t know. You’d need to find out that information from DIVI.

  • There isn’t any way to do this per page/post of the same post type. It is WP that is remembering and ordering the groups when you move them and it does this on a per/screen basis (post type, taxonomy, etc.). WP stores this value in the _usermeta table with meta_key of meta-box-order_{$screen}. There are no filter hooks available that would allow altering this order directly.

    There might be a way to modify this value.

    WP calls this to get the option

    
    $sorted = get_user_option( "meta-box-order_$page" );
    

    There is a filter in get_user_option()

    
    return apply_filters( "get_user_option_{$option}", $result, $option, $user );
    

    It should be possible to alter the order on the “page” post type with a filter

    
    add_filter('get_user_option_meta-box-order_page', 'YourFunctionName', 10, 3)
    

    Exactly how you you would alter the returned value I couldn’t say without actually building something and testing it.

  • Yes, That is exactly how I meant! Thank you. The only thing now is that I realized I will have to make a nested repeater to get several download sections with different headlines. I’m not 100% sure I know how to solve that. Can you help me? This is my code as it looks right now. The headline field is in the first repeater “file_sections”.

    $current_user = wp_get_current_user();
    
        echo '<div class="download_wrapper">';
    
            if( have_rows('file_sections', 'options_customer') ):
    
                while ( have_rows('file_sections', 'options_customer') ) : the_row();
    
                    echo '<div class="file-section">';
    
                        $download_headline = get_sub_field('download_headline', 'options_customer');
                        $download_preamble = get_sub_field('download_preamble', 'options_customer');
    
                        if ( $download_headline ) {
                            echo '<div class="file-title-section">';
                                echo '<h3>' . $download_headline . '</h3>';
                                echo '<p>' . $download_preamble . '</p>';
                            echo '</div>';
                        }
    
                        if( have_rows('section_download', 'options_customer') ):
    
                            ob_start();
                            $has_output = false;
    
                            echo '<ul class="download-list">';
    
                                while ( have_rows('section_download', 'options_customer') ) : the_row();
    
                                    //The file
                                    $file              = get_sub_field('client_file');
                                    //File restricted to
                                    $file_restriction  = get_sub_field('role_restriction');
                                    //Current user group
                                    $wcb2b_current_user_group = get_the_author_meta( 'wcb2b_group', $current_user->ID );
                                    
                                    if( in_array($wcb2b_current_user_group, $file_restriction ) || empty($file_restriction) ) { 
                                       $has_output = true;
                                       
                                       echo '<li class="download-item animateUp">';
    
                                            echo '<a target="_blank" title="Ladda ner fil: ' . $file['title'] . '" alt="Ladda ner fil: ' . $file['title'] . '" href="' . $file['url'] . '"></a>';
    
                                            echo '<div class="item-wrapper">';
                                                if( $file['type'] == 'image' ):
                                                    echo '<div class="item-image">';
                                                        echo '<img src="' . $file['sizes']['thumbnail'] .'" alt="'. $file['alt'] . '" width="50" height="auto">';
                                                    echo '</div>';
                                                endif;
                                                
                                                echo '<div class="item-content">';
                                                    echo '<h3>' . $file['title'] . '</h3>';
                                                    echo '<span>' . $file['modified'] . '</span>';
                                                echo '</div>';
                                            echo '</div>';
    
                                            $hr_size = size_format( $file['filesize'] );
                                            echo '<div class="item-size">' . $hr_size .'</div>';
    
                                        echo '</li>';
                                    }
    
                                endwhile;
    
                            echo '</ul>';
    
                            $output = ob_get_clean();
                            if ($has_output) {
                                echo $output;
                            }
    
                        endif;
    
                    echo '</div>';
    
                endwhile;
    
            endif;
    
        echo '</div>';
  • I think I understand the question.

    You want to show the entire thing, including the header, if any of the lines in the repeater are for the user.

    I would use a PHP output buffer like this

    
    ob_start();
    $has_output = false;
      
    echo '<h3>' . $download_headline . '</h3>';
    
    while ( have_rows('section_download', 'options_customer') ) : the_row();
    
        //File restricted to
        $file_restriction  = get_sub_field('role_restriction');
        //Current user group
        $wcb2b_current_user_group = get_the_author_meta( 'wcb2b_group', $current_user->ID );
        
        if( in_array($wcb2b_current_user_group, $file_restriction ) ) { 
           $has_output = true;
           // generate content
        }
    
    endwhile;
    
    $output = ob_get_clean();
    if ($has_output) {
      echo $output;
    }
    
  • Have you even tried the code?

    I’m sorry, I’m doing my best to help you but you’re not really helping yourself here.

    Your topic title doesn’t match your question
    Your initial question is a bit vague and mentioned hiding the heading – which I did
    Your initial code is not complete

    The code I’ve provided hides the title based on a user role – you restrict it by amending which roles are in the array
    It also hides specific files based on the user role

    You’ve not really provided any other details, so trying to provide a solution without all the information.

    Unlike a WP Query, you can’t query a repeater as such. Therefore, you need to loop through the repeater, if it meets your requirements i.e. visible to specific roles, then show the files.

    For people to assist you, you need to assist them! Provide a clearer question, provide your code and outline exactly what you wish to achieve.

    Based on the information provided, the supplied code does what you’ve asked – hides the heading and files based on a user role.

  • Is it a custom stylesheet?
    How are you enqueuing the stylesheet?

    You need to provide more info!

  • The below should achieve just that:

    <?php
    global $post;
    $author_id = $post->post_author;
    
    $user = get_userdata( $author_id );
    
    // Get all the user roles as an array.
    $user_roles = $user->roles;
    
    $author_roles = array('administrator', 'editor', 'contributor');
    
    if( have_rows('section_download', 'options_customer') ):
    
    	if( array_intersect($author_roles, $user->roles ) ) :
    		echo '<h3>' . $download_headline . '</h3>';
    	endif;
    	echo '<ul>';
    	while ( have_rows('section_download', 'options_customer') ) : the_row();
    
    		//File restricted to
    		$file_restriction  = get_sub_field('role_restriction');
    		//Current user group
    		$wcb2b_current_user_group = get_the_author_meta( 'wcb2b_group', $current_user->ID );
    
    		$restricted_file  = get_sub_field('restricted_file');
    		$non_restricted_file  = get_sub_field('non_restricted_file');
    
    		if( array_intersect($author_roles, $user->roles ) ) : ?>
    		<li><a href="<?php echo $restricted_file['url']; ?>"><?php echo $restricted_file['filename']; ?></a></li>
    		<?php endif; ?>
    		
    		<li><a href="<?php echo $non_restricted_file['url']; ?>"><?php echo $non_restricted_file['filename']; ?></a></li>
    
    	<?php endwhile;
    	echo '</ul>';
    endif;

    You’ve not included all of your code! You haven’t shown the file fields being declared so made up some for the example.

    Does that help?

  • You asked:

    I would like to hide the headline outside the loop

    I assumed $download_headline was therefore outputting the headline.

    As such, the code solution I gave did exactly that!

    Given the code provided, this gets the user role of the logged in person – this much I believe would be required to ensure your initial code works.

    You can therefore wrap your file inside the loop with the same conditional:

    if( array_intersect($author_roles, $user->roles ) ) :
    	//YOUR FILE
    endif;
  • That’s not the problem or the question. The problem is that I want to check my loop for files that are restricted to some user roles. If there aren’t any files to my user role, I shouldn’t see the headline. I can still see other files and access the page.

  • Can you provide more info?

    How is the data being added to the contents being added to lc_photos_list in the first instance?

    If you then add info to lc_registration, does it matter what the post ID will be?

    Based on your code, something like:

    add_action( 'woocommerce_payment_complete_order_status_completed', 'lc_complete_for_status' );
    
    function lc_complete_for_status( $order_id ){
    	
    	if( have_rows('lc_photos_list', 579) ):
    		 while( have_rows('lc_photos_list', 579) ) : the_row();
    	
    			$sub_value = get_sub_field('sub_field');
    	
    			$post_id = 576;
    			$row = array(
    				'field_61645b916cbd7'	=> $sub_value #this is your repeater field key
    			);
    			$i = add_row('field_61645b866cbd6', $row, $post_id); #this is the key for the main repeater	
    	
    		endwhile;
    	endif;
    	
    	#update_field("lc_photos_list", $copyPhotosObject, 576);
    	
    }

    I don’t know the repeater field info or how you get the post IDs (579/576)

    Should hopefully get you underway.

    Code is untested!

  • Hi @dedalos-01

    <?php
    if ( is_user_logged_in() ) :
    
    	$user_id = get_current_user_id();
    	$file= get_field( 'file', "user_".$user_id );
    	if($file): ?>
    		<a href="<?php echo $file['url']; ?>"><?php echo $file['filename']; ?></a>	
    	<?php endif;
    
    endif;
  • Hi @nicorubrixcreations-nl

    You need to loop the product data and add it to the repeater. Something like:

    <?php
    function create_post_after_order( $order_id ) {
    	
    	if ($order_id instanceof WC_Order ){
    		return;
    	}
    
    	$order = wc_get_order( $order_id );
    	$order_items = $order->get_items(); 
    	
    	$products = array();
    	
    	foreach ( $order_items as $item_id => $item_data ) {
    		$product	= $item_data->get_product();
    		$qty		.= $item_data->get_quantity();
    		$name		.= $product->get_name();
    		
    		$products[] = array(
    			'product'	=> $product,
    			'qty'		=> $qty
    		);
    	}
    
    	$new_post = array(
    		'post_title' => "Order {$order_id}",
    		'post_content' => $content,
    		'post_status' => 'private',
    		'post_date' => date('Y-m-d H:i:s'),
    		'post_author' => $user_ID,
    		'post_type' => 'groeiproces',
    		'post_status' => 'publish',
    	);
    	$post_id = wp_insert_post($new_post);
    
    	/*
    	$class_field_key = 'field_61645b866cbd6';
    	$class_subfield_name = 'field_61645b916cbd7';
    	$class_subfield_colour = 'field_6165450699ffa';
    	$class_names = array($name,$name);
    	$class_colours = array($qty,$qty);
    	
    	foreach ($class_names as $index => $class_names) {
    		$class_value[] = array($class_subfield_name => $class_names, $class_subfield_colour => $class_colours[$index]);
    		update_field( $class_field_key, $class_value, $post_id );
    	}
    	*/
    
    	echo '<pre>';
    	print_r($products); #check the data in the array. 
    	echo '</pre>';
    	
    	if( $products ):
    		foreach( $products as $product ):
    	
    			$row = array(
    				'field_61645b916cbd7'	=> $product[0],		#product field key
    				'field_6165450699ffa'	=> $product[1],		#qty field key
    			);
    			$i = add_row('field_61645b866cbd6', $row, $post_id); #this is the key for the main repeater
    
    	
    		endforeach;
    	endif;
    
    }
    add_action( 'woocommerce_thankyou', 'create_post_after_order', 10, 1 );

    You need to check/change the key values. The code is also untested but hopefully will get you on the right lines.

    Basically, add the product data to a new array. Then loop that data into your repeater.

  • HI @reason8

    You can access the data from a user using:

    
    <?php
    if ( is_user_logged_in() ) :
    
    	$user_id = get_current_user_id();
    	$value = get_field( 'my_field', "user_".$user_id );
    	if($value):
    		echo $value;
    	endif;
    
    endif;
    

    You’d just need to add that in to your template

  • HI @bigrat95

    Could you not just add a loop to your page template, loop through all pages/posts and output the ACF field?

    
    <?php
    $args = array(
    	'posts_per_page'	=> -1,
    	'post_type'			=> 'post',
     	'orderby' 			=> 'date',
        'order'   			=> 'DESC',	
    	'paged'				=> $paged,
    	'fields'			=> 'ids'			
    );
    $wp_query = new WP_Query($args);
    if ($wp_query->have_posts()) :
    	while ($wp_query->have_posts()) : $wp_query->the_post();
    
    	$pdf_upload = get_field('pdf_upload');
    	if( $pdf_upload ): ?>
           <a href="<?php echo $pdf_upload['url']; ?>">Manuel PDF</a>
        <?php endif; 
    
    	endwhile;
    endif;
    
  • Thanks @hube2
    We did manage to identify the issue. An editorial request was to make the Excerpt field mandatory, which was causing issues with the acf-field post_type saves via the wp_insert_post_data filter. Once we located that and put an exclusion there for this post_type regular service resumed.
    It was definitely a weird one, and I appreciate the advice. Originally I had checked all save_post filters, it was a colleague who found it in the end.

  • This is correct. There isn’t any way to get all of the values for all of the posts using any function in ACF or WP. To do what you want to do you would have to query the _postmeta table in the DB directly.

    https://developer.wordpress.org/reference/classes/wpdb/

    edit: in addition to this, this field in the DB only contains the ID value of the media file (upload) and you would need to get the link to this file yourself. https://developer.wordpress.org/reference/functions/wp_get_attachment_url/

  • 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' );
      }
    }
  • Is it possible? Yes. Coding will be required.

    See this topic https://support.advancedcustomfields.com/forums/topic/category-taxonomy-in-repeater-not-saving/

    This is the most recent, but this is a common question.

  • You need to check that you are only applying it to the post type in question.

    
    if (empty($query->query_vars['post_type']) || $query->query_vars['post_type'] != 'your-post-type') {
      return;
    }
    
Viewing 25 results - 5,201 through 5,225 (of 21,399 total)