How to display only the parent owner of an attachment pdf file (using AC and ElasticPress)

Question

I have a CPT (using ACF fields), that have a title, some other taxonomies and a pdf attached file (it’s also an attachment file because i need that Elasticpress search in it).
When I do a search, when a word is in the CPT title and in the PDF content, I have 2 results in my list. But it is the same CPT (the CPT and it’s pdf). I only need to retrieve the CPT title (that contains the pdf file). So I’m lost…..

<?php

        $args = [
            'post_type' => 'resources',
            'post_per_page' => 0,
            'tax_query' => [],
            'meta_query' => [
                'relation' => 'AND',
            ],
        ];

        if (isset($_GET['keyword'])) {
            if (!empty($_GET['keyword'])) {
                $args['s'] = $_GET['keyword'];
            }
        }

        if (isset($_GET['authors'])) {
            if (!empty($_GET['authors'])) {
                $args['tax_query'][] = [
                    'taxonomy' => 'authors',
                    'field' => 'slug',
                    'terms' => array($_GET['authors'])
                ];
            }
        }

        if ($is_search) {
            $query = new WP_Query($args);
        } ?>

        <?php if ($is_search): ?>
            <?php if ($query->have_posts()) : ?>

                <?php while ($query->have_posts()) : $query->the_post();

                    $file = get_field('resource_pdf_file');
                    $link = $file['url'];
                    $icon = $file['icon'];
                    $size = $file['filesize'];
                    $hr_size = size_format($size);
                    ?>

                    <article class="resources-item-list">
                        <img src="<?php echo $icon; ?>" alt="pdf icon">
                        <h4>
                            <a href="<?php echo $link; ?>"
                               target="_blank"><?php echo get_the_title($post->post_parent);
                                ?></a>
                        </h4>
                        <p><?php echo $hr_size ?></p>
                    </article>

enter image description here

The First result is the attachment match and the third is the Parent post that contain the attachment.

Thanks if someone can help…!

0
Perrine MARTYRIS 3 months 0 Answers 15 views 0

Leave an answer