boonebgorges / buddypress-docs

GNU General Public License v3.0
106 stars 44 forks source link

Sort folders by most recent update of contained doc #688

Open dcavins opened 3 years ago

dcavins commented 3 years ago

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 commented 3 years ago

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

pgrafix commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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