Support

Account

Home Forums Front-end Issues Field Type "User", only show the posts that the "User" is selected

Solving

Field Type "User", only show the posts that the "User" is selected

  • I have created a Custom Post Type called Updates, and setup ACF Field Type “User” so when a new post is created you can select a user. I cannot figure out how to show ONLY the posts that have been selected for the user that is logged in. Please advise.

    I don’t see any documentation on the Field Type “User”

  • Hi @sbrenner7

    You can find the currentl logged in use with this WP code:
    http://codex.wordpress.org/Function_Reference/wp_get_current_user

    After you have the user ID, you can then perform a query on the WP posts via the get_field function.

    You can find info on that here:
    http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/

    The meta_name is the field name, and the meta_value is the user ID selected

    Thanks
    E

  • Thanks Elliot, however I’m having a really difficult time putting this all together, my custom post type is ‘caregiver-updates’ and the advanced custom field that selects the user name is ‘user_id’…

    What I need to happen is if the user is logged in it will show all the custom post types of ‘caregiver-updates’ that have the advanced custom field user name ‘user_id’ selected, so somehow check if current user id logged in matches the custom field ‘user_id’ and then pull just those posts, please advise. Thank you so much!!!!

  • Hi @sbrenner7

    Can you explain where these posts will appear on the website? Is this on a specific page, or perhaps in the footer?

    Firstly, please find the appropriate template file to add the code.

    Next, research via google / WP how to get the current logged in user ID.
    I have already provided a link above to get you started

    Next, use this ID to perform a get_posts query and load the posts based on the custom field value.

    Good luck

    Thanks
    E

  • Hi @elliot

    I have looked over the links you sent as well as researched but I’m having trouble putting it all together, I know how to code into my templates just not having luck getting the code right to do the following…

    Page is called Updates and shows custom post types ‘caregiver-updates’, I have it working fine now that it shows ALL the posts… but I want to only show the posts that are assigned to the logged in user by using the Advanced Custom Field User ‘user_id’, it needs to be dynamic incase a new user is created.

    See Screen shot of the Advanced Custom Field “User(s) to Share this Update” has the Field name of ‘user_id’

    Front end URL to this page is http://caretocontinue.staging.wpengine.com/caregiver-updates/

  • Hi @sbrenner7

    The fact that your user field is a multi-select is a large factor which will effect the code.

    If the field was a sinlgle select, you could perform a simple meta_name / meta_query lookup, but because it is now saved as a serialized array, you will need to allow for this in the meta_query param.

    Please look over example 3 on this page:
    http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/

    It should how you can find posts based on a value, which has been selected by a custom field which saves it’s data as an array.

    Perhaps you can post your code too.

    Thanks
    E

  • Hey @elliot

    I really don’t need a multi-select, I can change that. If it were a single selection could you provide me with the code to do what I’m needing? I thank you so much for all your help!!!

  • This is what I have so far and its not pulling any posts…

    <?php
    
    // args
    $args = array(
    	'numberposts' => -1,
    	'post_type' => 'caregiver-updates',
    	'meta_key' => 'user_id',
    	'meta_value' => $current_user->ID
    );
    
    // get results
    $the_query = new WP_Query( $args );
    
    // The Loop
    ?>
    <?php if( $the_query->have_posts() ): ?>
    	<ul>
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<li>
    			<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    		</li>
    	<?php endwhile; ?>
    	</ul>
    <?php endif; ?>
    
    <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>
  • Hi @sbrenner7

    Please debug the $args array to find out if all the data is correct like so:

    
    <?php 
    
    echo '<pre>';
    	print_r($args);
    echo '</pre>';
    die; ?>
    

    Place this after you have defined the $args array values.

    Thanks
    E

  • Hi @elliot

    I have placed that code and this is my outcome…

    Array
    (
        [numberposts] => -1
        [post_type] => caregiver-updates
        [meta_key] => user_id
        [meta_value] => 3
    )
  • Hi @sbrenner7

    Can you please confirm that the data is saved as “3” in the DB for the wp_postmeta row where meta_key = ‘user_id’?

    I have a feeling that becuase you originally had the field as a multi-select field, the data is saved as a serialized array.

    Please change the user field to a single select and resave the posts to update the values in the DB

    Thanks
    E

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

The topic ‘Field Type "User", only show the posts that the "User" is selected’ is closed to new replies.