Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #874345

    I want to show the posts with a certain tag first (“case”, it’s the only tag used if there is any), then by date (within the posts with the tag “case” and afterwards all others, each newest first).

    I added this to my functions.php but I don’t manage to get the date ordering too

    add_filter('avia_masonry_entries_query', 'rr_sort_by_tag', 10, 2);
    function rr_sort_by_tag($query, $params) {
        if(is_page(array(724, 3661, 3662))) {
            $query['orderby'] = "tag";
            $query['order'] = "ASC";
        }
        return $query;
    }

    Help :-) Thanks in advance!

    Best regards,
    Jurgen

    #875561

    Hey Jurgen,

    You can try the code like this

    
    add_filter('avia_masonry_entries_query', 'rr_sort_by_tag', 10, 2);
    function rr_sort_by_tag($query, $params) {
        if(is_page(array(724, 3661, 3662))) {
            $query['orderby'] = array(
    	    'tag' => 'asc',
                '{your date field}' => 'asc'
            );
        }
        return $query;
    }
    

    If you need further assistance please let us know.
    Best regards,
    Victoria

    #875889

    Hi,

    Thanks for your reply!

    I tried the code below, but no success… I tried both “post_date” and “date” but not difference…

    add_filter('avia_masonry_entries_query', 'rr_sort_by_tag', 10, 2);
    function rr_sort_by_tag($query, $params) {
        if(is_page(array(724, 3661, 3662))) {
            $query['orderby'] = array(
    	    'tag' => 'ASC',
                'post_date' => 'DESC'
            );
        }
        return $query;
    }

    Thanks for checking!

    Best regards,
    Jurgen

    #876500

    Hi,

    The “tag” is not a valid orderby parameter. You can use the “meta_value” as the orderby parameter but you need to add a custom field for each posts.

    // https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    Best regards,
    Ismael

    #876601

    Hi Ismael,

    So then I should do something like this: create a custom field for each post (for example “tag”) and then sort on this field with some code like this? How about the sort per tag, can this be newest first?

    add_filter('avia_masonry_entries_query', 'rr_sort_by_tag', 10, 2);
    function rr_sort_by_tag($query, $params) {
        if(is_page(array(724, 3661, 3662))) {
            $query['orderby'] = "meta_value";
            $query['metakey'] = "tag";
            $query['order'] = "DESC";
        }
        return $query;
    }

    Thanks!

    #877355

    Hi,

    Please try this filter.

    add_filter('avia_masonry_entries_query', 'rr_sort_by_tag', 10, 2);
    function rr_sort_by_tag($query, $params) {
      if( is_page( array( 724, 3661, 3662 ) ) ) {
        $include = array();
        $first = array();
    
        $tagged_args = array(
          'meta_key'     => 'tag_field',
        	'meta_compare' => '<='
        );
    
        $tagged = get_posts( $tagged_args );
    
        foreach($tagged as $tag) {
      		$first[] = $tag->ID;
      	}
    
        $args = array(
    	  'taxonomy' => $params['taxonomy'],
      	);
    
      	$posts = get_posts( $args );
    
      	foreach($posts as $post) {
      		$include[] = $post->ID;
      	}
    
      	$include = array_merge($first, $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'] = 12;
      	$query['orderby'] = 'post__in'; // sort items based on the post__in value
      }
    
      return $query;
    }

    We query the posts with the custom field “tag_field” and then prepend them to rest of the posts.

    Best regards,
    Ismael

    #878041

    Hi Ismael,

    Thank you for the code!

    Unfortunately I only see 6 or 7 posts while there are 30 posts. The posts with a custom field “tag_field” (I mentioned this for 2 of my posts) do appear first but I need them all to appear… When I switch to the 2nd and 3rd language I only see the posts with the custom field, and none of the others (while in English I do see a few)…

    I assume we are almost there… Thanks again!

    Best regards,
    Jurgen

    #878407

    Hi,

    Please provide a link to the actual page where you’re testing the filter. And provide the WP and FTP details in the private field. We would like to test the modification.

    Best regards,
    Ismael

    #883425

    Hi Ismael,

    Please check the information in the private part to investigate this.

    Best regards,
    Jurgen

    #883766

    Hi,

    Thank you for the info.

    I was testing the filter and the site goes down. I’m not sure why. I just modified the functions.php file.

    What are the possible values in the “tag_field” custom field aside from “case”?

    Best regards,
    Ismael

    #883767

    Hi,

    UPDATE: I found the issue with the code. The site is up again.

    The filter is also working as it should. Please let us know if that’s how you want it.

    Best regards,
    Ismael

    #883841

    Hi Ismael,

    Thank you so much for helping me out on this!! I just added modified the order so the tagged posts are as well sorted by newest first.

    For others reading this topic, this is the final code :-)

    function rr_sort_by_tag($query, $params) {
      if( is_page( array( 724, 3661, 3662 ) ) ) {
        $include = array();
        $first = array();
    
        $tagged_args = array(
        	'meta_key'   => 'tag_field',
    //    	'orderby'    => 'meta_value',
    //    	'order'      => 'ASC',
    	'orderby'  => array(
    		'meta_value' => 'ASC',
    		'post_date'  => 'DESC',
    	),
    
        	'meta_query' => array(
        		array(
        			'key'     => 'tag_field',
        			'value'   => array( 'case', 'another' ),
        			'compare' => 'IN',
        		),
        	),
          'posts_per_page' => -1
        );
    
        $tagged = get_posts( $tagged_args );
    
        foreach($tagged as $tag) {
      		$first[] = $tag->ID;
      	}
    
        $new = array(
            'post__not_in' => $first
      	);
    
        $new = array_merge($query, $new);
    
      	$posts = get_posts( $new );
    
      	foreach($posts as $post) {
      		$include[] = $post->ID;
      	}
    
      	$include = array_merge($first, $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['orderby'] = 'post__in'; // sort items based on the post__in value
      }
    
      return $query;
    }
    add_filter('avia_masonry_entries_query', 'rr_sort_by_tag', 10, 2);
    
    #884223

    Hi,
    Thank you for sharing your solution, shall we will close this now?

    Best regards,
    Mike

    #884586

    Hi Mike,

    Yes you can do that :)

    Best regards,
    Jurgen

    #884834

    Hi,

    Glad to hear that. Thanks for using Enfold :)

    Best regards,
    Nikko

Viewing 15 posts - 1 through 15 (of 15 total)
  • The topic ‘Custom sort of posts’ is closed to new replies.