Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #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,
    Daniel

    #1492311

    try:

    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

    #1492318

    Hi,

    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,
    Ismael

    #1492339

    Hi 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,
    Daniel

    #1492368

    And 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.

    #1492370

    by 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);
    #1492388

    Hi 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.

    #1492389

    Hi,

    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,
    Ismael

    #1492400

    Hi 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 shown

    #1492401

    do you replaced the “your-cpt” with the name of your CPT. (f.e.: event …)

    #1492405

    Yes: angebot

    #1492448

    Hi,

    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,
    Ismael

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.