The Definitive Guide to Setting Up WordPress in 2024: A Bristol Freelance Web Developer’s Essential Setup
In the ever-evolving landscape of website development, staying ahead of the curve...
There are other ways to do this, but just in case you fancied using an ACF field, this is how you do it…it worked for me!
Lets say you have built a custom news feed for your WordPress site and you want to loop through three different custom post types. For this example we have three groups of posts – “posts”, “publications” and “events”.
<?php
$args = array( 'post_type' => array( 'post', 'publications', 'events' ),
'orderby' => 'date',
'order' => 'DESC',
'showposts' => 10
);
$loop = new WP_Query( $args );
if( $loop->have_posts() ): ?>
Now let say you have created a custom ACF select field called “Business Sector” with various values, and you want to only loop through the posts with the value of “charities”:
<?php
$args = array( 'post_type' => array( 'post', 'publications', 'events' ),
'orderby' => 'date',
'order' => 'DESC',
'showposts' => 10,
'meta_query' => array(
array(
'key' => 'business_sector',
'value' => 'charities',
'compare' => 'LIKE'
)
)
);
$loop = new WP_Query( $args );
if( $loop->have_posts() ): ?>
You can replace “key” and “value” to match your own custom field.
Useful links: http://www.advancedcustomfields.com/resources/filter-posts-by-custom-fields/
ACF, custom post type, Wordpress, WordPress Tips
Share
In the ever-evolving landscape of website development, staying ahead of the curve...
In the digital realm, your website serves as the cornerstone of your...
In the ever-evolving world of e-commerce, having a robust and flexible platform...
In the ever-evolving world of e-commerce, having a robust and flexible platform is crucial for businesses to thrive online. When...
Get the featured image dimensions (width and height) in WordPress. Paste the following code in the single.php file. <?php $post_id...
Here is a really simple way to create custom tabs for your WooCommerce builds using the ACF Repeater Field. First,...