How to hide companies in companies section?

If there is a company in my company page on my site, but the job has already expired or they already found a person for their job, how can I make it so the company is hidden?

Because I can imagine a situation where there are a bunch of companies listed in my companies section of my site, but when a user clicks on it, it will just say “no results” because the job they posted already expired.

How can I hide the companies (in the companies section) that don’t have any active jobs available? And only show the ones which have an active job listing?

Sorry, there’s no such option in the current version, when the last listing of a company is expired or hidden the company profile is not hidden or deleted. We can try to provide a temporary code snippet for this, but it may require further customizations.

Ok, yeah if you could provide a code snippet for this, that would be great.

Are there any plans to update the theme and make this a permanent feature of the theme? I think this would make the user experience a lot better.

Because I think it will be annoying for users to look in the companies section and keep clicking on company after company with no jobs.

There should be a way to only show the companies with a current job post.

Hi,

Thanks for your suggestion, we’ll consider adding this feature.

To show companies with current job posts, if I understand you correctly, please try these PHP snippets (replace “one, two, three” with the field names you want to keep in sync with the vendor fields):

add_action(
	'hivepress/v1/models/vendor/update',
	function( $vendor_id, $vendor ) {

		// Check status.
		if ( $vendor->get_status() !== 'publish' ) {
			return;
		}

		// Set attributes.
		$attributes = array_flip( [ 'one', 'two', 'three' ] );

		// Get values.
		$values = array_intersect_key( $vendor->serialize(), $attributes );

		// Get listings.
		$listings = HivePress\Models\Listing::query()->filter(
			[
				'status__in' => [ 'draft', 'pending', 'publish' ],
				'vendor'     => $vendor->get_id(),
			]
		)->get();

		// Update listings.
		foreach ( $listings as $listing ) {
			if ( array_intersect_key( $listing->serialize(), $attributes ) !== $values ) {
				$listing->fill( $values )->save( array_keys( $values ) );
			}
		}
	},
	10,
	2
);

add_action(
	'hivepress/v1/models/listing/update',
	function( $listing_id, $listing ) {

		// Get vendor.
		$vendor = $listing->get_vendor();

		if ( ! $vendor || $vendor->get_status() !== 'publish' ) {
			return;
		}

		// Get attributes.
		$attributes = array_flip( [ 'one', 'two', 'three' ] );

		// Get values.
		$values = array_intersect_key( $vendor->serialize(), $attributes );

		// Update listing.
		if ( array_intersect_key( $listing->serialize(), $attributes ) !== $values ) {
			$listing->fill( $values )->save( array_keys( $values ) );
		}
	},
	10,
	2
);
1 Like

Thank you so much. Should I input this code in this section:
Appearance —>Theme file editor ------> Theme functions

Is that the correct spot?

Hi,

Please try using the Code Snippets plugin for adding and managing custom PHP code snippets.

Ok, I just tried it but I didn’t notice any effect. I put in your code and activated it but there was no change.

I have one test company with no jobs but the company still shows up in the companies page.

Also, I don’t really understand this part “replace “one, two, three” with the field names you want to keep in sync with the vendor fields” so I just kept it the same.

Could this be the issue?

Hi,

Please try this PHP snippet:


add_action(
 'hivepress/v1/models/listing/update',
 function( $listing_id, $listing ) {

  // Get vendor.
  $vendor = $listing->get_vendor();

  if ( ! $vendor ) {
   return;
  }
  
  $vendor_listing_count = \HivePress\Models\Listing::query()->filter([
   'vendor' => $vendor->get_id(),
   'status' => 'publish',
  ])->get_count();
  
  if(!$vendor_listing_count){
   if($vendor->get_status() !== 'publish'){
    return;
   }
   
   $vendor->set_status( 'draft' )->save_status();
  }else{
   if($vendor->get_status() === 'publish'){
    return;
   }
   
   $vendor->set_status( 'publish' )->save_status();
  }
 },
 10,
 2
);

Same as before unfortunately. I uploaded this new php snippet in the PHP section of the snippets plugin and I there was no effect.

That same test company with no jobs is showing up in the companies section.

Please try to update any of the company listings, this snippet is triggered only when a listing is published or unpublished, just activating the snippet will not show/hide companies automatically.

I just tried again. I created a company called “test company” with no jobs and it’s still showing up first in the companies page.

Please try this PHP snippet instead

add_action(
	'hivepress/v1/models/listing/update',
	function( $listing_id, $listing ) {

		// Get vendor.
		$vendor = $listing->get_vendor();

		if ( ! $vendor ) {
			return;
		}
  
		$vendor_listing_count = \HivePress\Models\Listing::query()->filter([
			'vendor' => $vendor->get_id(),
			'status' => 'publish',
		])->get_count();
  
		if(!$vendor_listing_count){
			if($vendor->get_status() !== 'publish'){
 				return;
			}
   
			$vendor->set_status( 'draft' )->save_status();
		}else{
			if($vendor->get_status() === 'publish'){
 				return;
			}
   
			$vendor->set_status( 'publish' )->save_status();
		}
	},
 	10,
 	2
);

add_action(
	'hivepress/v1/models/vendor/update',
	function( $vendor_id, $vendor ) { 
		$vendor_listing_count = \HivePress\Models\Listing::query()->filter([
			'vendor' => $vendor_id,
			'status' => 'publish',
		])->get_count();
  
		if(!$vendor_listing_count){
			if($vendor->get_status() !== 'publish'){
 				return;
			}
   
			$vendor->set_status( 'draft' )->save_status();
		}else{
			if($vendor->get_status() === 'publish'){
 				return;
			}
   
			$vendor->set_status( 'publish' )->save_status();
		}
	},
 	10,
 	2
);
1 Like

This didn’t seem to work either. After using this snippet, I created a fake company call “test company” but when I click publish, nothing happens.

And when I created a test job, “test company” didn’t show up in the company section of the new job.

It seems like this snippet just prevents me from being able to create a new company in the wordpress dashboard.

When I turned off this snippet then I was able to publish the company. But that leaves me with the same issue of companies showing up with no available jobs.

It was tested locally, and it seems to be ok.

The code snippet works in this way:

  1. If you create a vendor in the admin panel or the non-vendor user tries to become a vendor by adding a listing or with the direct vendor registration, then this code snippet checks if this vendor has at least one listing. If there is any published listing that belongs to this vendor, then the vendor gets a status Draft and becomes not visible on the frontend for other users

  2. If the vendor’s listing becomes invisible to other users (for example, it passes the expiration date, which is set in the Expiration Date attribute for the listing) and the vendor does not have other published listings, then the vendor gets a status Draft and becomes not visible on the frontend for other users
    If the vendor’s listing becomes visible for other users (for example, you approve some listing to publish), then the vendor gets a status Publish and becomes visible on the frontend for other users.

Does it work for you, or is something missing here?

Hmmm, I’m still not seeing it work the way I want it to. Here’s the process I took.

  1. I uploaded the code to the snippets plugin in the PHP section.
  2. I created a “test company” and noticed I was unable to publish. It created a draft like you said.
  3. I created a “test job” and tried to add this “test company” as it’s official company but it didn’t show up (because it’s still a draft).

If I can’t publish a company, then I can’t add any jobs to it. And this code seems to make it impossible to publish a company.

Let me know if I’m doing something wrong.

This code snippet works mostly for vendors. So you as admin can only create vendors which will get draft status as they do not have any listings. If the vendor will add some listing and you approve it (if you have this restriction), then the vendor becomes published. It works in this way in general. Unfortunately, there is no simple code snippet to make it more flexible, it requires advanced customization. But it is possible to use this code snippet for further customization. If you are not familiar with the code customization then please consider hiring someone for custom work https://fvrr.co/32e7LvY

By vendor I assume you mean company, right?

“If the vendor will add some listing and you approve it (if you have this restriction), then the vendor becomes published. It works in this way in general.”

But when a user wants to create an account and add a listing, they will have to create the company first. With this code implemented will the new users even be able to create a company?

Yes, new users can become vendors/company

Ok, in that case, thank you for creating the code but I can’t use it now. As my site is new, I have to upload all the companies and jobs myself. So for me to continue to do that, I can’t use your code (because it stops me from being able to publish companies).

Maybe in the future if I get to the point where all the new jobs and companies are created by the users themselves (and not me) then maybe this code will be useful.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.