• Resolved patriqueuiliam

    (@patriqueuiliam)


    Helo! I need a field to search users for their publications. That’s all I need to complete this part of my project. Can you help me?

    EXAMPLE: If the user enters the “post” field he is able to find the user who posted in Buddypress results.

    It could be a function, or something simple. I really need this to complete my work. Thanks.`

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter patriqueuiliam

    (@patriqueuiliam)

    Best example:

    The author of the post type publishes: “Tips on how to face the pandemic”

    visitor search: Pandemic

    And as a result all authors who published about pandemic appear in the buddypress directory

    Plugin Author Andrea Tarantini

    (@dontdream)

    Hello patriqueuiliam,

    Luckily, that feature was on my to-do list and I had even started working on it. Hopefully, it could be ready in a few weeks.

    Thread Starter patriqueuiliam

    (@patriqueuiliam)

    Hi Andrea! I just need this feature to finish my project. Could you help me with a simple function so I can do this, before you release a version with the feature?

    Thanks!

    Plugin Author Andrea Tarantini

    (@dontdream)

    Yes, you can add the following code to your bp-custom.php file. I’ll have to test it thoroughly before releasing it, but you can help me test it.

    add_filter ('bps_add_fields', 'bps_posts_setup', 99);
    function bps_posts_setup ($fields)
    {
    	$columns = array
    	(
    //		'ID'				=> 'integer',
    //		'post_author'			=> 'integer',
    		'post_date'			=> 'date',
    //		'post_date_gmt'			=> 'date',
    		'post_content'			=> 'text',
    		'post_title'			=> 'text',
    //		'post_excerpt'			=> 'text',
    		'post_status'			=> 'text',
    //		'comment_status'		=> 'text',
    //		'ping_status'			=> 'text',
    //		'post_password'			=> 'text',
    //		'post_name'			=> 'text',
    //		'to_ping'			=> 'text',
    //		'pinged'			=> 'text',
    //		'post_modified'			=> 'date',
    //		'post_modified_gmt'		=> 'date',
    //		'post_content_filtered'		=> 'text',
    //		'post_parent'			=> 'integer',
    //		'guid'				=> 'text',
    //		'menu_order'			=> 'integer',
    		'post_type'			=> 'text',
    //		'post_mime_type'		=> 'text',
    		'comment_count'			=> 'integer',
    	);
    
    	$columns = apply_filters ('bps_posts_columns', $columns);
    	foreach ($columns as $column => $format)
    	{
    		$f = new stdClass;
    		$f->group = __('Posts data', 'bp-profile-search');
    		$f->code = $column;
    		$f->name = $column;
    		$f->description = '';
    
    		$f->format = $format;
    		$f->options = array ();
    		$f->search = 'bps_posts_search';
    
    		$fields[] = $f;
    	}
    
    	return $fields;
    }
    
    function bps_posts_search ($f)
    {
    	global $wpdb;
    
    	$column = $f->code;
    	$filter = $f->format. '_'.  ($f->filter == ''? 'is': $f->filter);
    	$value = $f->value;
    
    	$sql['select'] = "SELECT DISTINCT post_author FROM {$wpdb->posts}";
    	$sql['where'] = bps_where ($column, $filter, $value);
    
    	$sql = apply_filters ('bps_field_sql', $sql, $f);
    	$query = $sql['select']. ' WHERE '. implode (' AND ', $sql['where']);
    
    	$results = $wpdb->get_col ($query);
    	return $results;
    }
    Thread Starter patriqueuiliam

    (@patriqueuiliam)

    Hi Andrea! I took the tests and the results still don’t show up.

    1) I installed a new WordPress for testing.
    2) I created the bp-custom.php file and placed the function.
    3) I created two publications and two users.
    4) I created a simple form to search the post title and author name.
    5) When I search for the post name, the results don’t appear.
    6) Here is the test URL: https://newrel.com/testtest/members/
    7) Here is the user… login: test password: pass
    8) I don’t know how to resolve this issue. ??

    Plugin Author Andrea Tarantini

    (@dontdream)

    Hi patriqueuiliam,

    This forum policy doesn’t allow me to access your site. If you wish, you can continue this topic on the dedicated BP Profile Search support forum:

    https://dontdream.it/support/forum/bp-profile-search-forum/

    Thread Starter patriqueuiliam

    (@patriqueuiliam)

    Solved by Andrea!!!

    add_filter ('bps_add_fields', 'bps_posts_setup', 99);
    function bps_posts_setup ($fields)
    {
    	$columns = array
    	(
    //		'ID'				=> 'integer',
    //		'post_author'			=> 'integer',
    		'post_date'			=> 'date',
    //		'post_date_gmt'			=> 'date',
    		'post_content'			=> 'text',
    		'post_title'			=> 'text',
    //		'post_excerpt'			=> 'text',
    		'post_status'			=> 'text',
    //		'comment_status'		=> 'text',
    //		'ping_status'			=> 'text',
    //		'post_password'			=> 'text',
    //		'post_name'			=> 'text',
    //		'to_ping'			=> 'text',
    //		'pinged'			=> 'text',
    //		'post_modified'			=> 'date',
    //		'post_modified_gmt'		=> 'date',
    //		'post_content_filtered'		=> 'text',
    //		'post_parent'			=> 'integer',
    //		'guid'				=> 'text',
    //		'menu_order'			=> 'integer',
    		'post_type'			=> 'text',
    //		'post_mime_type'		=> 'text',
    		'comment_count'			=> 'integer',
    	);
    
    	$columns = apply_filters ('bps_posts_columns', $columns);
    	foreach ($columns as $column => $format)
    	{
    		$f = new stdClass;
    		$f->group = __('Posts data', 'bp-profile-search');
    		$f->code = $column;
    		$f->name = $column;
    		$f->description = '';
    
    		$f->format = $format;
    		$f->options = array ();
    		$f->search = 'bps_posts_search';
    
    		$fields[] = $f;
    	}
    
    	return $fields;
    }
    
    function bps_posts_search ($f)
    {
    	global $wpdb;
    
    	$column = $f->code;
    	$filter = $f->format. '_'.  ($f->filter == ''? 'is': $f->filter);
    	$value = $f->value;
    
    	$sql['select'] = "SELECT DISTINCT post_author FROM {$wpdb->posts}";
    	$sql['where'] = bps_where00 ($column, $filter, $value);
    
    	$sql = apply_filters ('bps_field_sql', $sql, $f);
    	$query = $sql['select']. ' WHERE '. implode (' AND ', $sql['where']);
    
    	$results = $wpdb->get_col ($query);
    	return $results;
    }
    
    function bps_where00 ($column, $filter, $value)
    {
    	global $wpdb;
    
    	$where = array ();
    
    	switch ($filter)
    	{
    	case 'text_contains':
    		$value = stripslashes ($value);
    		$where[$filter] = bps_sql_expression ("{$column} LIKE %s", $value, true);
    		break;
    
    	case 'text_is':
    		$value = stripslashes ($value);
    		$where[$filter] = bps_sql_expression ("{$column} = %s", $value);
    		break;
    
    	case 'text_like':
    		$value = str_replace ('\\\\%', '\\%', $value);
    		$value = str_replace ('\\\\_', '\\_', $value);
    		$where[$filter] = bps_sql_expression ("{$column} LIKE %s", $value);
    		break;
    
    	case 'integer_is':
    		$where[$filter] = bps_sql_expression ("{$column} = %d", $value);
    		break;
    
    	case 'integer_range':
    		if (isset ($value['min']))  $where['min'] = $wpdb->prepare ("{$column} >= %d", $value['min']);
    		if (isset ($value['max']))  $where['max'] = $wpdb->prepare ("{$column} <= %d", $value['max']);
    		break;
    
    	case 'date_is':
    		$where[$filter] = bps_sql_expression ("DATE({$column}) = %s", $value);
    		break;
    
    	case 'date_range':
    		if (isset ($value['min']))  $where['min'] = $wpdb->prepare ("DATE({$column}) >= %s", $value['min']);
    		if (isset ($value['max']))  $where['max'] = $wpdb->prepare ("DATE({$column}) <= %s", $value['max']);
    		break;
    
    	case 'date_age_range':
    		$day = date ('j');
    		$month = date ('n');
    		$year = date ('Y');
    
    		if (isset ($value['max']))
    		{
    			$ymin = $year - $value['max'] - 1; 
    			$where['age_min'] = $wpdb->prepare ("DATE({$column}) > %s", "$ymin-$month-$day");
    		}
    		if (isset ($value['min']))
    		{
    			$ymax = $year - $value['min'];
    			$where['age_max'] = $wpdb->prepare ("DATE({$column}) <= %s", "$ymax-$month-$day");
    		}
    		break;
    	}
    
    	return $where;
    }

    CONTINUE IN: https://dontdream.it/support/forum/bp-profile-search-forum/

    THANK YOU ANDREA!

    Plugin Author Andrea Tarantini

    (@dontdream)

    You are welcome!

    I’ll release this feature as soon as possible, when I have completed all the tests.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Field to search users for a published post type’ is closed to new replies.