Sort views block by current node first


If you have a requirement where you need to display node of any content type, having value of one field should be in top and then rest value of that field from another node will come, this blog would help you to do so.

Sort views block by current node first might be using the module Views PHP.

Steps to create Sort views block by current node first:--

  • Create a block view to display content of your content type.
  • Format: no special settings
  • Fields:
    • Content: Nid
    • Content: Title
  • Filter Critera
    • Content: Published (Yes)
    • Content: Type (= YOUR CONTENT TYPE NAME)
  • Sort Criteria:
    • Content: Post date (desc)
    • Global: PHP (asc) (make sure to install Views PHP module)
      Configure the Views PHP filter similar to this: Sort Code (no php tags!):
      $nidFirst = arg(1);
      return $row1->nid != $nidFirst;

      Sort using views php image

Put the resulting block on YOUR CONTENT TYPE page and navigate to one of those pages.

As the sort direction is ascending, all rows with a comparison result of true will be moved below the row with the result false, so the current node ends up at the top of the list.

Add new comment