Support Forum
Hi,
Is it possible to filter Archive View by Custom Field?
For example, if I have a 'country_code' field. Can I choose to show 'country_code' that matches 'vn' ?
I tried something like below but the results are blank. Please advise.
{% if post.countryisocode == 'vn' %}
{% for post in query.posts %}
xxxx
{% endfor %}
{% endif %}
Hi Anson,
You should check the meta value of a post inside the loop because the archive page is a list of posts. The right code would be
{% for post in query.posts %}
{% if(post.countryisocode == 'vn') %}
{{ post.title }}
{% endif %}
{% endfor %}
Hi Long,
I am getting a blank result with the filter and I tried to filter using other custom fields as well.
I am using MB Custom Table..
Hi,
The code above works as well with the custom field which is saved in the custom table. See my screen record https://www.loom.com/share/0f554824d7304f6a9735c8cc26629014.
Hi Long,
Thank you so much for your help.
Your Loom video is very clear and helpful. I just checked with a few more random country codes and found that it is working.
However, strangely the posts that matches the countryisocode are spread across different pages (with most pages blank). I have pagination enabled and it shows 30 pages even though the actual number of 'VN' records is only 2. The custom table has 300 rows of data.
The records that matches 'VN' for 'countryisocode' can be found in these urls:
/archive-slug/page/1
/archive-slug/page/25/
I uploaded an example here : https://imgur.com/a/aEsXAXj
Hi,
Please share full code that you queries to show posts and pagination. I will take a closer look and give a quick response.
Hi Long,
I have shared the full code in MB View here : https://pastebin.com/qPHbPTPG
Hi Anson,
The function get_the_posts_pagination()
retrieves a paginated navigation to next/previous set of posts in the main query. In this case, the navigation shows all posts of the category. If you want to show the navigation for posts which have the specific meta countryisocode
, you have to query to get posts by WP_Query
then show the pagination with the custom query via PHP code.
For more information, please follow these documentations.
https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters
https://codex.wordpress.org/Pagination
https://developer.wordpress.org/reference/functions/get_the_posts_pagination/
Hi Long,
I have gone through the links and also took reference from the link below.
https://support.metabox.io/topic/meta_query-with-mb-custom-tables/
I am now trying to filter 'Cities' by country code ('us') obtain from the custom table.
Can you advise what's wrong with the following code? I am getting the result 'nothing here'.
<?php
global $wpdb;
$post_ids = $wpdb->get_col( $wpdb->prepare(
"SELECT ID FROM cities
WHERE countryisocode = 'us'",
$post->post_author
) );
$query_args = array (
'post_type' => 'city',
'orderby' => 'meta_value',
'meta_key' => 'countryisocode',
'order' => 'ASC',
'post__in' => $post_ids,
);
//The query
$cities_in_country = new WP_Query( $query_args );
// The Loop
if ( $cities_in_country->have_posts() ) {
echo '<ul>';
while ( $cities_in_country->have_posts() ) {
$cities_in_country->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
echo "nothing here";
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
Thank you.
Solved the problem with this!
<?php
global $wpdb;
$ids = $wpdb->get_col( "SELECT ID FROM cities WHERE countryisocode = 'cn'" );
if ( empty( $ids ) ) {
echo 'There is no posts';
} else {
$query = new WP_Query( [
'post_type' => 'city',
'post__in' => $ids,
] );
while ( $query->have_posts() ) {
$query->the_post();
echo get_the_title() . '<br>';
}
// Do something
}
?>