Support

Account

Home Forums General Issues Getting relationship field values into selectbox

Solving

Getting relationship field values into selectbox

  • Hi

    I have an issue that I encountered, and can’t figure it out. I have two post types: artists, projects.
    For artists I have added a relationship ACF field, that relates to the ‘projects’ posts. On the artists archive page I’ve created a search form, one of the fields is a selectbox that should list the relationship values.
    How can I fill that selectbox with the relationship field values?
    How should the WP_Query look like with the relationship field integrated?

    If someone did such a thing, or can post some code snippets that would be really helpful!

    Thanks

  • Hi @zkingdesign,

    we have create number of acf fields… i think this field can be helpful for you:

    https://github.com/reyhoun/acf-subfield-chooser

  • Hi Navid,

    Installed the plugin, but it only lists the relationship values from the first post.

  • this plugin can be only 2 option for get list:

    – get from a repeater field created in option page (ACF Pro)
    – get from a post (single post ID)

    i read again your problem, this field doesn’t for you.

    you can create a small query like relationship field and then create you select list. something like this:

    <?php $args = array(
    	'posts_per_page'   => 5,
    	'offset'           => 0,
    	'category'         => '',
    	'orderby'          => 'post_date',
    	'order'            => 'DESC',
    	'include'          => '',
    	'exclude'          => '',
    	'meta_key'         => '',
    	'meta_value'       => '',
    	'post_type'        => 'post',
    	'post_mime_type'   => '',
    	'post_parent'      => '',
    	'post_status'      => 'publish',
    	'suppress_filters' => true ); ?>
        
    <?php $posts_array = get_posts( $args ); ?>
    
    <select>
    <?php foreach ( $dynamicposts as $post ) : setup_postdata( $post ); ?>
      <option value="<?php echo get_permalink(); ?>"><?php echo get_permalink(); ?></option>
    <?php endforeach; ?>
    </select>
    
    <?php wp_reset_postdata(); ?> 
  • Thanks Navid,

    The code you posted is something that I have at the moment, yes it lists all of the projects, but the issue is that I can’t figure out what should I add in the WP_Query, tried 'post_type' => array('projects','artists') but the issue is that that code would list the ‘projects’ posts, and not the related posts.

  • you can you relation field value for filter query, use post__in parameter in query

  • Tried that aswell, as I said in my previous reply, but that simply returns the ‘Projects’ posts and not the ‘Artist’ posts that relate to the queried ‘Project’

  • for better understand, you create a page like sitename.com/artist-archive and in this page a select field with project post?

    Opportunity to share a screenshot to explain better there?

    tnx

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

The topic ‘Getting relationship field values into selectbox’ is closed to new replies.