BuddyDev

Search

[Resolved] Searching and re-ordering within a gallery.

Tagged: 

  • Participant
    Level: Initiated
    Posts: 12
    Paul on #3132

    I need to allow users to search for specific images within a gallery using info found in the title and description. I also need to programmatically sort images. Can you point me in the right direction here?

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #3133

    Hi Paul,
    That is very easy. Do you want the user to be able to search images from some custom page, if so, you can pass the “search_terms” parameter t search for media to the MPP_Media_Query.
    https://buddydev.com/mediapress/topics/api-reference/core/mpp_media_query/#parameters

    If you are not using it on custom page, Please let me know and I will assist you in passing it to our main queries if needed. The same applies for the order/orderby. Please take a look at the documentation.

  • Participant
    Level: Initiated
    Posts: 12
    Paul on #3145

    Hi Brajesh,

    I am using this on a gallery in which the images are added from buddypress fields using mpp_add_media() so I’m guessing since we are using this on a gallery page it’s not a custom page but at the same time all of the images added to this gallery are custom from buddypress fields so I’m not sure.

    When I interact with MPP_Media_Query class using one of the loop styles described in the documentation you suggested where would you suggest I place that ocde? In the same plugin I created to populate the gallery with mpp_add_media? Which action would you suggest I use? Would I create the actual search box with html? How do I retrieve the submitted vars? There is no explanation of the “$args” variable. Can you give me a bit more insight to this: “It uses parent::query() and only allows limited options with query.”

    Thanks,
    Paul

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #3151

    Hi Paul,
    I am sorry for the confusion. The parameters section explains the $args.

    In your case, you don’t need to write custom loop unless you are going for ajax search filters.

    We can modify the main query( I favor it to avoid extra database queries in this case) to account for search .

    1. Yes, you will need to modify the template and put a search form
    2. After that, we can put the following code

    
    
    function mpp_custom_use_search_args( $args ) {
    
    	//assuming your search form has input element 'search-terms'
    	if ( isset( $_REQUEST['search-terms'] ) ) {
    		$args['search-terms'] = $_REQUEST['search-terms'];
    	}
    	//Using 'search-terms' in args allows us to search usign MPP_media_Query
    	//$args['search-terms']
    
    	return $args;
    
    }
    add_filter( 'mpp_main_media_query_args', 'mpp_custom_use_search_args' );
    
    

    This filters our main media query. So, It will list the filtered media.

    Please do note that the approach to filter using the above filter will only work on standard MediaPress pages(which is the case for you).

    Please give it a try and let me know.

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 12
    Paul on #3160

    Brajesh,

    Thanks for your reply here. All works as expected except for it needs to be $args[‘search_terms’] instead of $args[‘search-terms’] (underscore, not a dash). Your reply was very helpful!

    Cheers,
    Paul

  • Participant
    Level: Initiated
    Posts: 12
    Paul on #3162

    Hey, Brajesh,

    We will need to sort these by a numeric bp_xprofile_field. Is that possible with this solution? the search is going fine and I see how I can order by meta_keys but how would I sort by meta_keys present in bp_xprofile_fields?

    Thanks,
    Paul

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #3163

    Hi Paul,
    Thank you.
    I am glad the search is working. Indeed, It was ‘search_terms’ and not ‘search-terms’.

    About reordering, I will advise storing the numeric data in the media meta(post meta) using mpp_update_media_meta or update_post_meta and then doing it by keys. Media is just an attachment in WordPress, so everything that applies to attachment can be applied here.

    Hope that helps.

The topic ‘ [Resolved] Searching and re-ordering within a gallery.’ is closed to new replies.

This topic is: resolved