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 #1492448 -
AuthorPosts
- You must be logged in to reply to this topic.
