Hello Support Team,
I am writing to report an issue I am experiencing while using the Geo Directory plugin on my WordPress website. I have encountered a problem with WP_Query not returning expected results when trying to filter posts based on the existence of a specific custom field (‘geodir_telefon’).
Here is the code that is functioning correctly and displaying all the data:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'gd_public_poz',
'posts_per_page' => 15,
'paged' => $paged
);
$custom_query = new WP_Query($args);
if ($custom_query->have_posts()) : ?>
<div class="post-wrapper">
<?php while ($custom_query->have_posts()) : $custom_query->the_post();
$title = get_the_title();
$post_id = get_the_ID();
$permalink = get_permalink();
$phone_value = get_post_meta($post_id, 'geodir_telefon', true);
$address_value = get_post_meta($post_id, "geodir_street", true);
$address_city = get_post_meta($post_id, "geodir_city", true);
$display_phone = ($phone_value) ? $phone_value : '';
$display_address = ($address_value && $address_city) ? $address_value . ', ' . $address_city : '';
?>
<div class="post-single">
<h1 class="post-single-title">
<a href="https://projectdmc.org/support/topic/issue-with-wp_query-and-geo-directory-plugin-filtering/<?php echo esc_url($permalink); ?>"><?php echo $title; ?></a>
</h1>
<?php if (!empty($display_phone)) : ?>
<p class="post-single-phone">
<i class="fas fa-phone fa-fw" aria-hidden="true"></i>
<b>Telefon:</b> <?php echo $display_phone; ?>
</p>
<?php endif; ?>
<?php if (!empty($display_address)) : ?>
<p class="post-single-address">
<i class="fas fa-map-marker-alt fa-fw" aria-hidden="true"></i>
<b>Adres:</b> <?php echo $display_address; ?>
</p>
<?php endif; ?>
<p class="post-single-link">
<a href="https://projectdmc.org/support/topic/issue-with-wp_query-and-geo-directory-plugin-filtering/<?php echo esc_url($permalink); ?>">Czytaj więcej</a>
</p>
</div>
<?php endwhile; ?>
</div>
<?php
$total_pages = $custom_query->max_num_pages;
if ($total_pages > 1) {
$current_page = max(1, get_query_var('paged'));
echo '<div class="custom-post-pagination">';
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
));
echo '</div>';
}
wp_reset_postdata();
else :
echo 'Brak postów spełniających kryteria.';
endif;
?>
However, the following arguments do not return the expected values:
$args = array(
'post_type' => 'gd_public_poz',
'posts_per_page' => 15,
'meta_query' => array(
array(
'key' => 'geodir_telefon',
'compare' => 'EXISTS',
),
),
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
);
$custom_query = new WP_Query($args);
The second query with the ‘meta_query’ parameter should ideally return posts where the ‘geodir_telefon’ custom field exists, but it’s not working as expected.
I have ensured that the custom field ‘geodir_telefon’ exists for the posts I am trying to retrieve, but the query does not seem to recognize it properly.
Could you please assist me in resolving this issue? Any guidance or support you can provide would be greatly appreciated.
Thank you in advance for your help.
Warm regards,
Maciej
The page I need help with: [log in to see the link]