Support

Account

Home Forums General Issues How to display related field from a selected post type

Unread

How to display related field from a selected post type

  • I am using ACF Relationship. I have 2 custom post types set as location and job. I need to show all available jobs for a location when a user select 1 location in the front end form.

    Here is my code which still needs help.

    	
    <form>
        <select name="joblocation-select" id="joblocation-select">
            <?php
                $locations = get_posts( 'post_type=wpsl_stores&numberposts=-1&post_status=publish&orderby=title&order=ASC' );
                foreach( $locations as $post ) : setup_postdata($post); ?>
                    <option value="<? echo $post->ID; ?>"><?php echo $post->post_title; ?></option>
                <?php endforeach; 
                    // wp_reset_postdata(); 
            ?>
        </select>
            <?php $locationid =  $_POST['joblocation-select']; //Set to variable selected location ?>
        <select name="jobopening-select" id="jobopening-select">
            <?php
            $args = array(
                'post_type' => 'job_opening',
                'numberposts' => '-1',
                'post_status' => 'publish',
                'orderby' => 'title',
                'order' => 'ASC',
                'meta_query' => array(
                    array(
                        'key' => 'job_location', // name of custom field
                        'value' => $locationid, // matches exactly "123", not just 123. This prevents a match for "1234"
                        'compare' => 'LIKE'
                    )
                )
            );
    
            $jobs = get_posts( $args );
            foreach( $jobs as $job ) : setup_postdata($job); ?>
                <option value="<? echo $job->ID; ?>"><?php echo $job->post_title; ?></option>
            <?php endforeach; 
                wp_reset_postdata(); 
            ?>
        </select>
    </form>
    
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.