Tagged: Custom Post Type, masonry, order, sticky posts
-
AuthorPosts
-
December 9, 2025 at 6:26 pm #1492304
Hi folks,
I found a nice piece of code in the forum:
add_filter('avia_masonry_entries_query', 'avia_masonry_entries_query_mod', 10, 1); function avia_masonry_entries_query_mod($query) { $query['post__in'] = get_option( 'sticky_posts' ); $query['ignore_sticky_posts'] = 1; return $query; }Now my sticky posts from a CPT show up at the top –> BUT the rest of the posts is missing. Only the sticky posts are part of the query, the remaining posts are not part of the masonry. That is wrong. Any idea why?
Kind regards,
DanielDecember 9, 2025 at 7:40 pm #1492311try:
add_filter('avia_masonry_entries_query', 'avia_masonry_entries_query_mod', 10, 2); function avia_masonry_entries_query_mod($query, $params) { $include = array(); $sticky = get_option( 'sticky_posts' ); $args = array( 'taxonomy' => $params['taxonomy'], 'post__not_in' => $sticky, ); $posts = get_posts( $args ); foreach($posts as $post) { $include[] = $post->ID; } $include = array_merge($sticky, $include); // convert values of the $include from string to int function sti($n) { settype($n, 'int'); return $n ; } $include = array_map("sti", $include); $query['post__in'] = $include; $query['posts_per_page'] = 6; $query['orderby'] = 'post__in'; // sort items based on the post__in value return $query; }see Ismaels post on : https://kriesi.at/support/topic/sticky-posts-not-displaying-forst/#post-1230187
December 10, 2025 at 7:27 am #1492318Hi,
Thank you for the info.
You can also try this code — it’s the same, just slightly modified.
add_filter( 'avia_masonry_entries_query', 'avia_masonry_entries_query_mod', 10, 2 ); function avia_masonry_entries_query_mod( $query, $params ) { if ( empty( $params['taxonomy'] ) ) { return $query; } $sticky = get_option( 'sticky_posts', array() ); $posts = get_posts( array( 'post_type' => 'any', 'posts_per_page' => -1, 'tax_query' => array( array( 'taxonomy' => $params['taxonomy'], 'field' => 'term_id', 'terms' => $params['terms'] ?? array(), ) ), 'post__not_in' => $sticky, 'fields' => 'ids', ) ); $include = array_unique( array_map( 'intval', array_merge( $sticky, $posts ) ) ); $query['post__in'] = $include; $query['posts_per_page'] = 6; $query['orderby'] = 'post__in'; return $query; }Best regards,
IsmaelDecember 10, 2025 at 9:07 am #1492339Hi Ismael,
I found this code already but the problem remains: Now my sticky posts from a CPT show up at the top –> BUT the rest of the posts is missing. Only the sticky posts are in the query, the remaining posts are not part of the masonry. I have one sticky post and without the code all posts are shown. With the code only the sticky one is visible. Same behaviour as with the code from my question …
Kind regards,
DanielDecember 10, 2025 at 9:26 pm #1492368And you used the new code of ismael ? – because the ( ‘post_type’ => ‘any’,) is important for using it with CPT …
or explicitly name it as a post type:how did you embed your cpt to masonry?
function custom_masonry_cpt_with_sticky($query, $params) { $query['post_type'] = array('post', 'portfolio', 'your-cpt'); $query['ignore_sticky_posts'] = 0; // Optional: if you like to adjust it // $query['posts_per_page'] = 12; return $query; } add_filter('avia_masonry_entries_query', 'custom_masonry_cpt_with_sticky', 10, 2);and then try:
function custom_masonry_sticky_first($query, $params) { $query['post_type'] = array('post', 'portfolio', 'your-cpt'); // Sticky Posts first $sticky = get_option('sticky_posts'); if (!empty($sticky)) { $query['post__not_in'] = isset($query['post__not_in']) ? array_merge($query['post__not_in'], $sticky) : $sticky; $query_sticky = $query; $query_sticky['post__in'] = $sticky; $query_sticky['ignore_sticky_posts'] = 1; unset($query_sticky['post__not_in']); $query['_sticky_query'] = $query_sticky; } $query['ignore_sticky_posts'] = 0; return $query; } add_filter('avia_masonry_entries_query', 'custom_masonry_sticky_first', 10, 2);enter your CPT for ” your-cpt” in the snippets.
December 10, 2025 at 9:53 pm #1492370by the way – for blog:
function custom_blog_cpt_with_sticky($query, $params) { $query['post_type'] = array('post', 'portfolio', 'your-cpt'); $query['ignore_sticky_posts'] = 0; return $query; } add_filter('avia_blog_post_query', 'custom_blog_cpt_with_sticky', 10, 2);December 11, 2025 at 9:07 am #1492388Hi Guenni, I used the code of Ismael. Result: Only the sticky post is shown. If I use your code only the non sticky posts are shown. I want the sticky posts to be shown first and the non sticky afterwards. As it is in the blog with normal posts.
December 11, 2025 at 9:08 am #1492389Hi,
Did you remove the previous code?
add_filter('avia_masonry_entries_query', 'avia_masonry_entries_query_mod', 10, 1); function avia_masonry_entries_query_mod($query) { $query['post__in'] = get_option( 'sticky_posts' ); $query['ignore_sticky_posts'] = 1; return $query; }Please make sure to remove this code, and then add any of the suggested code above.
Best regards,
IsmaelDecember 11, 2025 at 10:02 am #1492400Hi Ismael, I always tried one at a time:
Code from my opening post: Only sticky post is shown
Code from your post: Only sticky post is shown
Code from Guenni: Only non sticky posts are shownDecember 11, 2025 at 10:12 am #1492401do you replaced the “your-cpt” with the name of your CPT. (f.e.: event …)
December 11, 2025 at 10:41 am #1492405Yes: angebot
December 12, 2025 at 8:01 am #1492448Hi,
Thank you for the update.
We may need to inspect this to properly understand the issue. Please create a test page and provide the login details in the private field.
Best regards,
IsmaelDecember 22, 2025 at 9:35 am #1492743Have you solved the problem? If so, could you please report here how you did it?
January 12, 2026 at 3:44 pm #1493848Hi Guenni, thanks for asking. It was not working as expected, therefore I tried a different solution. I added a date field with ACF
termin_datumand want to sort all posts by this field. The posts without the field shall be sorted by the date of the post itself. I inserted this code from the documentation:if(!function_exists('avia_custom_query_extension')) { function avia_custom_query_extension($query, $params) { global $avia_config; if(!empty($avia_config['avia_custom_query_options']['order'])) { $query['order'] = $avia_config['avia_custom_query_options']['order']; } if(!empty($avia_config['avia_custom_query_options']['orderby'])) { if($avia_config['avia_custom_query_options']['orderby'] == 'termin_datum') { $query['orderby'] = [ 'meta_value' => 'DESC', // Sorting posts by ACF date in ascending order 'date' => 'DESC' // Fallback for posts without a date; these should come last ]; // Define ACF field $query['meta_key'] = 'termin_datum'; } else { $query['orderby'] = $avia_config['avia_custom_query_options']['orderby']; } } unset($avia_config['avia_custom_query_options']); return $query; } add_filter('avia_masonry_entries_query', 'avia_custom_query_extension', 10, 2); add_filter('avia_post_grid_query', 'avia_custom_query_extension', 10, 2); add_filter('avia_post_slide_query', 'avia_custom_query_extension', 10, 2); add_filter('avia_blog_post_query', 'avia_custom_query_extension', 10, 2); add_filter('avf_magazine_entries_query', 'avia_custom_query_extension', 10, 2); add_filter('avf_template_builder_shortcode_elements','avia_custom_query_options', 10, 1); function avia_custom_query_options($elements) { $allowed_elements = array('av_blog','av_masonry_entries','av_postslider','av_portfolio','av_magazine'); if(isset($_POST['params']['allowed']) && in_array($_POST['params']['allowed'], $allowed_elements)) { $elements[] = array( "name" => __("Custom Query Orderby",'avia_framework' ), "desc" => __("Set a custom query orderby value",'avia_framework' ), "id" => "orderby", "type" => "select", "std" => "", "subtype" => array( __('Default Order', 'avia_framework' ) =>'', __('Title', 'avia_framework' ) =>'title', __('Random', 'avia_framework' ) =>'rand', __('Date', 'avia_framework' ) =>'date', __('Author', 'avia_framework' ) =>'author', __('Name (Post Slug)', 'avia_framework' ) =>'name', __('Datum (ACF)', 'avia_framework' ) =>'termin_datum', __('Modified', 'avia_framework' ) =>'modified', __('Comment Count', 'avia_framework' ) =>'comment_count', __('Page Order', 'avia_framework' ) =>'menu_order') ); $elements[] = array( "name" => __("Custom Query Order",'avia_framework' ), "desc" => __("Set a custom query order",'avia_framework' ), "id" => "order", "type" => "select", "std" => "", "subtype" => array( __('Default Order', 'avia_framework' ) =>'', __('Ascending Order', 'avia_framework' ) =>'ASC', __('Descending Order', 'avia_framework' ) =>'DESC')); } return $elements; } add_filter('avf_template_builder_shortcode_meta', 'avia_custom_query_add_query_params_to_config', 10, 4); function avia_custom_query_add_query_params_to_config($meta, $atts, $content, $shortcodename) { global $avia_config; if(empty($avia_config['avia_custom_query_options'])) $avia_config['avia_custom_query_options'] = array(); if(!empty($atts['order'])) { $avia_config['avia_custom_query_options']['order'] = $atts['order']; } if(!empty($atts['orderby'])) { $avia_config['avia_custom_query_options']['orderby'] = $atts['orderby']; } return $meta; } }That works as expected, there is only one problem: The sorting order is wrong! Now it is shown this way in frontend:
7.2. (with filled ACF date fieldF)
31.1. (with filled ACF date field)
11.1. (with filled ACF date field)And after that the posts without the date field in correct order by publishing date:
2.1. (without ACF by publishing date)
30.12. (without ACF by publishing date)
24.12. (without ACF by publishing date)But I want it sorted like that:
11.1. (with filled ACF date field)
31.1. (with filled ACF date field)
7.2. (with filled ACF date field)
2.1. (without ACF by publishing date)
30.12. (without ACF by publishing date)
24.12. (without ACF by publishing date)If I try altering the ASC / DESC settings, the posts with ACF are shown after the posts without the filled acf field. Any ideas on why it is like that?
Thanks in advance and all the best wishes for 2026!
January 14, 2026 at 5:39 am #1493891Hi,
Thank you for the info.
In the ACF field termin_datum sorting block, try to adjust the parameters using the following code in order to sort the posts with the ACF field by the meta value.
if ($avia_config['avia_custom_query_options']['orderby'] === 'termin_datum') { $query['meta_query'] = array( 'relation' => 'OR', array( 'key' => 'termin_datum', 'compare' => 'EXISTS', 'type' => 'DATE' ), array( 'key' => 'termin_datum', 'compare' => 'NOT EXISTS' ) ); $query['orderby'] = array( 'meta_value' => 'ASC', 'date' => 'DESC' ); $query['meta_key'] = 'termin_datum'; $query['meta_type'] = 'DATE'; }Let us know the result.
Best regards,
IsmaelJanuary 14, 2026 at 4:39 pm #1493913Hi Ismael, same problem the sorting ist correct but the posts with the filled ACF field are at the end. I want them to be shown before the posts without the filled field. Any idea on how to keep the array in the first place?
January 14, 2026 at 11:29 pm #1493924can you test this instead ( and remove your solution temporarly ) :
/** * Enfold Masonry Filter: Show sticky posts first, followed by other entries (including CPTs) * This filter captures selected post types dynamically from the element settings. */ function avia_masonry_entries_query_mod($query, $params) { // Prevent the filter from running in the WordPress admin backend if (is_admin()) { return $query; } // 1. Get the IDs of all sticky posts $sticky = get_option('sticky_posts'); // If no sticky posts exist, return the original query without modifications if (empty($sticky)) { return $query; } // 2. Fetch IDs for the remaining posts (including Custom Post Types) // We use $params['post_type'] to dynamically support whatever is selected in the Masonry element $args = array( 'post_type' => $params['post_type'], 'post__not_in' => $sticky, // Exclude stickies so they aren't duplicated 'posts_per_page' => -1, // Fetch all IDs to ensure we have enough for the merge 'fields' => 'ids', // Performance optimization: only fetch IDs ); // If a specific taxonomy/category is selected in the element, apply it to our helper query if (!empty($params['taxonomy'])) { $args['tax_query'] = array( array( 'taxonomy' => $params['taxonomy'], 'field' => 'id', 'terms' => explode(',', $params['categories']), ), ); } $other_posts = get_posts($args); // 3. Merge the arrays: Stickies first, then the remaining posts $combined_ids = array_merge($sticky, $other_posts); // 4. Sanitize the IDs (ensure they are integers) $query['post__in'] = wp_parse_id_list($combined_ids); // 5. Force the Masonry to respect the exact order of our merged array $query['orderby'] = 'post__in'; $query['order'] = 'ASC'; // Set the final limit of items to display $query['posts_per_page'] = 6; // Disable default sticky handling to prevent WP from interfering with our custom order $query['ignore_sticky_posts'] = true; return $query; } add_filter('avia_masonry_entries_query', 'avia_masonry_entries_query_mod', 10, 2);January 14, 2026 at 11:41 pm #1493925if this works – here is the extended Way trying to have that for pagination or “load more” Masonry:
/** * ENFOLD MASONRY: Show Sticky Posts first in Blog/Portfolio Masonry only * (excludes Masonry Galleries) */ function avia_masonry_entries_query_mod($query, $params) { // Exit if admin if (is_admin()) return $query; // Exit if this is a Masonry Gallery if (isset($params['container_class']) && $params['container_class'] === 'av-masonry-gallery') { return $query; } // Exit if post_type is attachment (additional safety check) if (isset($query['post_type']) && $query['post_type'] === 'attachment') { return $query; } // Exit if no sticky posts exist $sticky = get_option('sticky_posts'); if (empty($sticky)) return $query; // Get the limit from element settings $per_page = isset($query['posts_per_page']) ? $query['posts_per_page'] : 6; // Fetch all non-sticky posts matching the query $args = array( 'post_type' => $params['post_type'], 'post__not_in' => $sticky, 'posts_per_page' => -1, 'fields' => 'ids', 'tax_query' => !empty($query['tax_query']) ? $query['tax_query'] : array(), ); $other_posts = get_posts($args); // Merge sticky posts first, then others $all_ids = array_merge($sticky, $other_posts); // Apply the ordered IDs to the query $query['post__in'] = wp_parse_id_list($all_ids); $query['orderby'] = 'post__in'; $query['order'] = 'ASC'; $query['posts_per_page'] = $per_page; $query['ignore_sticky_posts'] = true; return $query; } add_filter('avia_masonry_entries_query', 'avia_masonry_entries_query_mod', 10, 2);EDIT: i changed the code with a check if it is an image masonry – because it should not influence these masonries.
January 14, 2026 at 11:45 pm #1493926here is a way to have the option by classes to style those sticky posts by an own selector:
/** * Add a custom CSS class to sticky posts for styling purposes */ function add_sticky_classes( $classes, $class, $post_id ) { if ( is_sticky($post_id) ) { $classes[] = 'is-sticky-post'; $classes[] = 'stickypost-' . $post_id; } return $classes; } add_filter( 'post_class', 'add_sticky_classes', 10, 3 );/* Style sticky items in Masonry */ .is-sticky-post .av-inner-masonry::after { content: 'Featured'; position: absolute; top: 10px; right: 10px; background: #000; color: #fff; padding: 2px 8px; font-size: 10px; text-transform: uppercase; z-index: 5 }January 15, 2026 at 9:50 am #1493963Hi Guenni, thank you for the code but as mentioned in post #149384 I stepped away from a solution with sticky posts because it is cleaner for me to sort by ACF field first and sort the other posts later. In this sorting is an error as explained above (#1493913) and I dont know why the order is the wrong way around. Do you have an idea?
January 16, 2026 at 5:58 am #1494009Hi,
What is the format of the termin_datum date field? Have you tried setting the format to Ymd or Y-m-d? WordPress meta_query with the type “DATE” expects the value in YYYY-MM-DD format or YYYYMMDD, so adjusting the date field format might help.
— https://developer.wordpress.org/reference/classes/wp_meta_query/
The ‘type’ DATE works with the ‘compare’ value BETWEEN only if the date is stored at the format YYYY-MM-DD and tested with this format.
Best regards,
IsmaelJanuary 16, 2026 at 8:04 am #1494011@spooniverse: I understand that, but it would be nice to check whether my code above works; since I don’t use CPT, I was reluctant to install one just to test my assumptions above.
January 19, 2026 at 4:52 pm #1494071Hi Isamel, it is the same wrong sorting behaviour with the date field in Ymd or Y-m-d (testet both ways). Any other ideas?
Hi Guenni, I will try to find the time to do a test for you but it will not be a solution for my wrong ordering at the moment …
January 20, 2026 at 5:31 am #1494082Hi,
Did you apply the changes we recommended here https://kriesi.at/support/topic/cpt-sticky-posts-in-masonry/#post-1493891 before adjusting the date format? If you can create a test and provide the login details in the private field, we’ll try to take a closer look.
Best regards,
Ismael -
AuthorPosts
- You must be logged in to reply to this topic.
