Filter Archive View by Custom Field
- This topic has 10 replies, 2 voices, and was last updated 4 years, 9 months ago by
Anson.
-
AuthorPosts
-
July 21, 2020 at 12:04 AM #20858
Anson
ParticipantHi,
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 %}
July 21, 2020 at 8:43 AM #20863Long Nguyen
ModeratorHi 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 %}
July 21, 2020 at 9:32 AM #20864Anson
ParticipantHi 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..
July 21, 2020 at 3:06 PM #20871Long Nguyen
ModeratorHi,
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.
July 21, 2020 at 3:49 PM #20874Anson
ParticipantHi 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/July 21, 2020 at 4:13 PM #20875Anson
ParticipantI uploaded an example here : https://imgur.com/a/aEsXAXj
July 21, 2020 at 9:25 PM #20884Long Nguyen
ModeratorHi,
Please share full code that you queries to show posts and pagination. I will take a closer look and give a quick response.
July 21, 2020 at 10:03 PM #20886Anson
ParticipantHi Long,
I have shared the full code in MB View here : https://pastebin.com/qPHbPTPG
July 22, 2020 at 8:51 AM #20892Long Nguyen
ModeratorHi 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 metacountryisocode
, you have to query to get posts byWP_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/July 22, 2020 at 4:19 PM #20903Anson
ParticipantHi 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.
July 22, 2020 at 6:29 PM #20908Anson
ParticipantSolved 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 } ?>
-
AuthorPosts
- You must be logged in to reply to this topic.