• Resolved yeochinsoon

    (@yeochinsoon)


    I am using buddypress membership. Is there a short code on show ads that are created ONLY by the logged in member? thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    do you mean to show the selected user ads on his BP profile page? If so then this can be done with the BuddyPress extension https://wpadverts.com/extensions/buddypress-integration/

    If you want to use the [adverts_list] shortcode to show only ads posted by a logged in users then you can add the code below in your theme functions.php file

    
    add_filter( "shortcode_atts_adverts_list", function( $out, $pairs, $att ) {
        if(isset($att["loggedin_only"])) {
            $out["loggedin_only"] = absint( $att["loggedin_only"] );
        } else {
            $out["loggedin_only"] = 0;
        }
        return $out;
    }, 10, 3 );
    
    add_filter( "adverts_list_query", function( $args, $params ) {
        if( $params["loggedin_only"] == 1 ) {
           $args["author__not_in"] = array( 0 ); 
        }
        return $args;
    }, 10, 2 );
    

    and then use the [adverts_list] with a loggedin_only param like in the example below to show the ads posted by logged-in users.

    
    [adverts_list loggedin_only="1"]
    
    Thread Starter yeochinsoon

    (@yeochinsoon)

    Thank you very much. it works when i used [adverts_list author=”1″].

    Thread Starter yeochinsoon

    (@yeochinsoon)

    I need more flexibility in the above.

    In the documentation,parameter:- the author: int (default: null) (since 1.0.8) – user ID, setting this parameter allows listing all Ads posted by a selected user

    It is possible to use “user ID” instead of “1”?

    Thank you.

    Plugin Author Greg Winiarski

    (@gwin)

    I have received your question via email so you should get a reply via email as well soon, please do not double post your questions, thanks.

    Anyway, to filter by the user id dynamically on the BP profile pages you can use the adverts_list_query filter like this

    
    add_action( "adverts_list_query", function( $args ) {
      if( bp_displayed_user_id() > 0 ) {
        $args["author"] = bp_displayed_user_id();
      }
      return $args;
    } );
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Listing of ads by user_id’ is closed to new replies.