Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort folders by most recent update of contained doc #688

Open
dcavins opened this issue May 3, 2021 · 7 comments
Open

Sort folders by most recent update of contained doc #688

dcavins opened this issue May 3, 2021 · 7 comments

Comments

@dcavins
Copy link
Collaborator

dcavins commented May 3, 2021

I think this is a good idea: https://wordpress.org/support/topic/re-ordering-folders/

It could be accomplished by changing the modified date of the enclosing folder(s) when a doc is updated or by adding "modified" meta to the folder. Boone, do you have a preference? I guess I'm leaning toward changing the modified date because we're not using it for anything.

@boonebgorges
Copy link
Owner

Modified date of the folder object sounds good to me. Thanks for looking into this!

@pgrafix
Copy link

pgrafix commented Jul 25, 2023

I have read this and #689 and it relates to what a client of mine is requesting of me (and quickly): the ability to set default sort order per folder. This would include an option of menu order sorting. Please let me know if this is possible and whether this should be requested as a standalone feature request.

@dcavins
Copy link
Collaborator Author

dcavins commented Jul 25, 2023

Hi @pgrafix Sure, it would be possible to set the default sort order for each specific folder (only to be used when viewing a single folder--I don't think it would make sense to change the sort order if you were looking at three expanded folders for instance.) by attaching meta to the post that defines the folder and then using that meta to change the order in the query if no sort order has been specified. It's not high on my list to implement, but I do believe that it would be possible.

@pgrafix
Copy link

pgrafix commented Aug 1, 2023

@dcavins Thank you for the response, David. In the meantime would it possible to get a snippet that would allow us to manually target a folder?

@pgrafix
Copy link

pgrafix commented Aug 15, 2023

I think I am close, but perhaps I am targeting the wrong hook:

add_filter( 'bp_after_bp_docs_has_docs_parse_args', function ( $args ) {
	$folder_id = bp_docs_get_current_folder_id(); 
	echo '<script type="text/javascript">console.log('.json_encode($folder_id).');</script>'; // Make sure $folder_id is being retrieved
		if ( $folder_id==='target_folder_id' && empty( $_GET['orderby'] ) ) {
			$orderby = 'created';
			$args['orderby'] = $this->orderby;
			$args['order'] = $this->ascdsc;
}
return $args;
} );

As I said, I just need to target a single folder for default ordering of by creation date instead of title. Any help would be greatly appreciated.

@boonebgorges
Copy link
Owner

@pgrafix It looks like you're probably targeting the correct hook, but your references to $this will always fail inside of a callback like this, and your check for 'target_folder_id' obviously will need to be swapped out. So something like:

add_filter( 
  'bp_after_bp_docs_has_docs_parse_args', 
  function ( $args ) {
    $current_folder_id = bp_docs_get_current_folder_id();
    $folder_id_to_target = 12345; // Replace with your folder ID
    if ( $current_folder_id !== $folder_id_to_target ) {
      return $args;
    }

    if ( ! empty( $_GET['orderby'] ) ) {
      return $args;
    }

    $args['orderby'] = 'created';
    $args['order'] = 'DESC';

    return $args;
  }
);

@pgrafix
Copy link

pgrafix commented Aug 16, 2023

I appreciate your response @boonebgorges - I knew I was missing something. Thanks again for all of your assistance.

UPDATE:

  1. I have updated the code to use an array of ids to target since we need this to apply to multiple, but not all, folders:
add_filter( 
  'bp_after_bp_docs_has_docs_parse_args', function ( $args ) {
    $current_folder_id = bp_docs_get_current_folder_id();
    $folder_ids_to_target = array( 1234, 5678 ); // Replace with your folder IDs
	  
if ( !in_array( $current_folder_id, $folder_ids_to_target ) ) {
	return $args;
 	}  

    if ( ! empty( $_GET['orderby'] ) ) {
      return $args;
    }

    $args['orderby'] = 'created';
    $args['order'] = 'DESC';

    return $args;
  }
);
  1. Whether using your original code or my modified version it does order as intended. However, visibly it shows the ordering is by Last Edited:
Screen Shot 2023-08-21 at 3 34 55 PM

In fact, we seem to be encountering a default orderby of Last Edited for all folders even though code has not been set to make it so and the default orderby for folders is by title based on the code in addon-folders.php. Where is orderby stored when not empty? Clearing cache, cookies and site-settings in the browser doesn't seem to affect this [return to default of by title].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants