Support

Account

Home Forums Front-end Issues ACF Search in Post Object

Solving

ACF Search in Post Object

  • Hello!
    Need help!
    I need to create a search form for Custom Post Type(CPT) with ACF. I have CPS ‘events’, each post has two ACF(type post object) for the other post( CPT ‘teams’).

    I need to search for the ‘events’ post by the ‘name'(done), and by the ‘team’ name (which I take from the post object by the ACF ).
    Can someone help me with this?

    <form action="" method="GET" id="archive_form">
        <div class="form-group col-xs-12">
            <label>Tournament</label>
            <input type="text" class="form-control" id="tournament" name="tournament_name"
                   placeholder="Type Tournament Info">
        </div>
        <div class="form-group col-sm-6">
            <label>Team 1</label>
            <input type="text" class="form-control" id="team_1" name="team_1"
                   placeholder="Type Team Name">
        </div>
        <div class="form-group col-sm-6">
            <label>Team 2</label>
            <input type="text" class="form-control" id="team_2" name="team_2"
                   placeholder="Type Team Name">
        </div>
        <div class="col-xs-12 text-right">
            <button type="submit" class="btn btn-success">Search</button>
        </div>
    </form>
    
    <?php
    function go_filter(){
    
        $_name = $_GET['tournament_name'] != '' ? $_GET['tournament_name'] : '';
        $_team_1 = $_GET['team_1'] != '' ? $_GET['team_1'] : '';
        $_team_2 = $_GET['team_2'] != '' ? $_GET['team_2'] : '';
    
        $args = array(
                'post_type' => 'events',
                's' =>  $_name,);
        $args['meta_query'] = array('relation' => 'AND');
        global $wp_query;
    
        if ($_team_1 != '') {
            $postobject = get_sub_field('team_1');
            $args['meta_query'][] = array(
                array(
                    'key' => 'team_1',
                    'value' => $_team_1
                )
            );
        }
    
        if ($_team_2 != '') {
            $args['meta_query'][] = array(
                'key' => 'team_2',
                'value' => $_team_2
            );
        }
    
        query_posts(array_merge($args, $wp_query->query));
    }
    
    ?>
  • A post object field only stores the post ID of the post object and not the name. Since the name of the post is not associated with the posts you are searching (“events”) you cannot search by the name.

    To do this you need to create an acf/save_post filter. In this filter you get the related post object and update some other field to hole the post name and then you search using this other custom field. I usually do not use an ACF field for this and just use update_post_meta() and use a meta key that will not conflict with other field names.

  • Thanks for your answer!
    Can you tell me more about update_post_meta() and meta key?

  • Since you are using a post object field, you could use a select field for your search where the value of each option is $post->ID and the value of the field is $post->post-title and then you can do a meta query on the post object field using the post ID instead of the post title.

  • I have found(with your help) other solution.
    I use acf/save_post filter, and save info to the CF from the sub-post(post_object, named Team) to the main post(named Event)

  • I have been changed post_object to the Relationship and have one problem.
    When I load category all information load good, but when I trying to search I have disappeared one field (the second field send me empty array ). Where is have a mistake?

    Function for filter

    <?php
    function go_filter()
    {
        $_name = $_GET['tournament_name'] != '' ? $_GET['tournament_name'] : '';
        $args = array(
            'post_type' => 'events', // your CPT
            's' => $_name
        );
        $args['meta_query'] = array('relation' => 'AND');
        global $wp_query;
    
        if ($_GET['team_1'] != '') {
            $args['meta_query'][] = array(
                'key' => 'filter_team_1',
                'value' => $_GET['team_1'],
            );
        }
        if ($_GET['team_2'] != '') {
            $args['meta_query'][] = array(
                'key' => 'filter_team_2',
                'value' => $_GET['team_2'],
            );
        }
        query_posts(array_merge($args, $wp_query->query));
    }
    
    ?>

    And page content

    <?php if ($_GET && !empty($_GET)) {
                                go_filter();
    
                            } ?>
                            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                                <?php $team_1 = get_field('team_1'); ?>
                                <?php $team_2 = get_field('team_2'); ?>
                                <li>
                                    <div class="team">
                                        <?php echo get_the_post_thumbnail($team_1[0]); ?>
                                        <?php echo get_the_title($team_1[0]); ?>
                                    </div>
                                    <div class="separate">VS</div>
                                    <div class="team">
                                        <?php echo get_the_post_thumbnail($team_2[0]); ?>
                                        <?php echo get_the_title($team_2[0]); ?>
                                    </div>
                                    <div class="time"><?php the_field('game_time'); ?></div>
                                    <a href="<?php the_field('game_link'); ?>">Tournament</a>
                                </li>
                            <?php endwhile;
                            else : ; ?>
                                <h4>Sorry, nothing matched your search criteria!</h4>
                            <?php endif; ?>
    
                            <?php wp_reset_query(); ?>
  • And is it possible to create some function that will do the next
    When I chose some post(post 2) in a relationship in Post 1 it will automatically choose Post 1 in a relationship in Post 2?

    You understand what I mean?)))))

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

You must be logged in to reply to this topic.