How to stop some specific published listings from being listed?

I wanna prevent a publically published Ad from being listed in the main listing page, while at the same time, anyone can view it through the permalink (e.g. http://mydomain.com/listing/xxx).

I wanna select such listing based on the value of a specific attribute or the listing’s ID.

Saving a listing as Draft isn’t the solution, because it makes the listing’s link inaccessible and invisible to the public too!

I found a solution given by ihor in this topic, but it also makes the listing’s link inaccessible and removes the Ad from anywhere, even from the author’s listings!

So please help me how to do this task.

Hi @hamid,

If I understand your request correctly, I think Ihor’s reply further down the page on the topic you linked to may be of use to you:

add_filter(
	'hivepress/v1/templates/listing_view_block/blocks',
	function ( $blocks, $template ) {
		$listing = $template->get_context( 'listing' );

		if ( $listing ) {
			$blocks = hivepress()->template->merge_blocks(
				$blocks,
				[
					'listing_container' => [
						'attributes' => [
							'id' => 'listing-' . $listing->get_id(),
						],
					],
				]
			);
		}

		return $blocks;
	},
	1000,
	2
);

Once you add the PHP snippet above to your site, each Listing block will have a unique ID which you can then use to target the specific Listings you want. You can then use a CSS snippet like the following example to hide them from search results:

#listing-123,
#listing-456,
#listing-789 {
	display: none !important;
}

Notes:

  • Update 123,456, and 789 with your actual listing IDs.
  • You’ll need to inspect each Listing block’s source code to identify the unique ID.

The result will be the specified Listing blocks will be hidden from search results, while still being fully accessible by linking to them, or visiting their URL directly.

I hope this helps!

Cheers,
Chris :victory_hand:

1 Like

Thank you very much ChrisB

I checked the solution and it worked, but it has at least two problems!

The first and the more important is that, using this CSS-based solution leaves a whole blank space between the visible Ads, which is not visually perfect at all!

We need a solution similar to the previous ihor’s solution (@ihor) which basically puts the desired listings out of the search results, but as I mentioned above, that code makes the listing’s link inaccessible and invisible to the public too!

add_action(
	'pre_get_posts',
	function( $query ) {
		if ( is_admin() || ( ! $query->is_main_query() && ! $query->get( 'hp_main' ) ) ) {
			return;
		}

		$query->set( 'post__not_in', [ 1, 2, 3 ] );
	},
	1000
);

Any other idea?

Hi @hamid,

I haven’t had a chance to test these yet, but AI suggests:

#listing-123,
#listing-456,
#listing-789 {
	display: none !important;
}

/* Adjust the selector to match your grid container */
.hp-listings {
	display: grid;
	grid-auto-flow: dense; /* This makes items flow to fill gaps */
}

Or

#listing-123,
#listing-456,
#listing-789 {
	display: none !important;
}

/* Adjust the selector to match your grid container */
.hp-listings {
	display: flex;
	flex-wrap: wrap;
}

Or, an updated/scoped version of Ihor’s snippet:

add_action(
	'pre_get_posts',
	function( $query ) {
		if ( is_admin() || ( ! $query->is_main_query() && ! $query->get( 'hp_main' ) ) ) {
			return;
		}

		// Only exclude listings from search/archive pages, not singular pages
		if ( ! is_singular( 'hp_listing' ) ) {
			$query->set( 'post__not_in', [ 1, 2, 3 ] );
		}
	},
	1000
);

I hope one of these suggestions works for you. Let us know how you get on! :slight_smile:

Cheers,
Chris :victory_hand:

Hi agian

I don’t know who AI is or what AI is or how much knowledge he has, but anyway I tested the customized version of ihor’s code and got the same result; the direct link of the desired Ads become disabled and out of access!

I can’t understand why the code, which works based on the search query of listings, also affects the direct link?! Is an Ad’s direct link a type of search query?!

If we can know this, we may be able to solve the problem by customizing the code!

Hi @hamid,

Edited:

AI is short for Artificial Intelligence, e.g tools like ChatGPT. HivePress also have their own AI Assistant.

I’ve had a chance to test this now, and the following solution works for me:

  1. Add the following snippet to assign a unique ID to each listing block:
add_filter(
	'hivepress/v1/templates/listing_view_block/blocks',
	function ( $blocks, $template ) {
		$listing = $template->get_context( 'listing' );

		if ( $listing ) {
			$blocks = hivepress()->template->merge_blocks(
				$blocks,
				[
					'listing_container' => [
						'attributes' => [
							'id' => 'listing-' . $listing->get_id(),
						],
					],
				]
			);
		}

		return $blocks;
	},
	1000,
	2
);
  1. Add this second snippet, and replace 1, 2, 3 with the unique Listing IDs of the Listings that you wish to hide.
add_action(
	'pre_get_posts',
	function ( $query ) {
		if ( is_admin() || ( ! $query->is_main_query() && ! $query->get( 'hp_main' ) ) ) {
			return;
		}

		// Skip singular requests so direct listing URLs keep working.
		if ( $query->is_singular() ) {
			return;
		}

		// Only target browsing contexts (archives, search, and the Listings page).
		if ( ! $query->is_main_query() && ! $query->get( 'hp_archive' ) ) {
			return;
		}

		$query->set( 'post__not_in', [ 1, 2, 3 ] );
	},
	1000
);

I hope this helps!

Cheers,
Chris :victory_hand:

Hi,

I hope you had a great weekend.

@ChrisB, thanks for help!

@hamid, please note that while we’re always happy to help with bug reports and issues related to the core functionality, developer guidance, custom implementation advice, and code snippets are covered as part of our premium support.

For customizations, you can try using our AI assistant together with the snippets shared by Ihor or Chris, or consider reaching out to one of our verified freelancers who can help implement the desired functionality: Customize your website | HivePress.

Thanks for your understanding

1 Like

Thanks a lot ChrisB, you’re really great! :heart_eyes:

The query code works well and unlists the desired Ads while keeps their direct links accessible.

Although this code satisfies my need, but let’s move a step further!

This solution needs to modify the hivepress code each time we wanna unlist an Ad, which isn’t standard and straightforward enough!

So what if we can select the Ads using the value of a specific attribute, e.g. a checkbox attribute (Yes or No)?

Do you think this can be done just by a simple modification of the current code or it needs a more complicated code?

Thanks again for your help and support!

1 Like

Hi kseniia

Thanks for your point!

Would you please be more direct and clear?

Do you mean that I’ve asked a lot of questions, more than allowed? Or do you mean this type of questions are out of scope of this community and must be paid to be solved by your support team or your experts? Both of them?! None of them?!

* : is ChirsB a member of Hivepress team?

TIA

I apologize if my previous response came across as confusing.

There’s no limit on how many posts or replies community members can write, and there’s nothing you’re “not allowed” to ask about, as long as our community guidelines are respected. We’re really glad to have an open, helpful community. For example, Chris isn’t officially a member of our team [it honestly feels like he is], but he’s an active community member who builds with HivePress and shares our values, and he genuinely enjoys helping others who need a hand.

All I wanted to convey is that while we try to help with every request, some of them can fall outside our support scope, since custom logic isn’t part of our built-in features and often requires deeper investigation on our end. We’re always happy to share sample code snippets when there’s a straightforward solution, and we provide more in-depth developer guidance for premium support users.

We also try to make solutions as accessible as possible, you’ll find plenty of code snippets, workarounds, and helpful topics across our community and documentation. On top of that, we have a growing collection of code snippets and an AI assistant available to all users, to make customization easier for everyone.

More advanced developer guidance can sometimes fall outside our support scope. Even so, we always do our best to help however we can: offering direct assistance, pointing you to verified experts, or guiding you to the right documentation.

Once again, my apologies for not making myself clear from the beginning.

1 Like

Hi @hamid,

You’re welcome, and I’m glad we managed to find a solution that works for you in the end.

I’m sure others in the community will find it useful in time, too.

Regarding your enhanced approach, yes, that’s possible, and it’s probably a better approach than hardcoding listing IDs.

Two steps:

  1. Create the attribute
  • Go to Listings → Attributes → Add Attribute:
  • Title it something like “Hidden” and check the slug is hidden. The slug becomes the meta key with an hp_ prefix (dashes become underscores), so hidden → hp_hidden. If you use a different slug, adjust the snippet below to match.
  • In the Editing section, set the field type to Checkbox.
  • If only site admins should control this, leave “Editable” unticked, and the checkbox will appear only on the back-end listing edit screen. Tick “Editable” if listing owners should be able to hide their own listings.
  • Leave the Search section options unticked.
  1. Replace the second snippet from my previous reply with this one:
add_action(
	'pre_get_posts',
	function ( $query ) {
		if ( is_admin() || ( ! $query->is_main_query() && ! $query->get( 'hp_main' ) ) ) {
			return;
		}

		// Skip singular requests so direct listing URLs keep working.
		if ( $query->is_singular() ) {
			return;
		}

		// Only target browsing contexts (archives, search, and the Listings page).
		if ( ! $query->is_main_query() && ! $query->get( 'hp_archive' ) ) {
			return;
		}

		// Keep any existing meta clauses (e.g. attribute filters).
		$meta_query = (array) $query->get( 'meta_query' );

		// Exclude listings with the checkbox ticked.
		$meta_query[] = [
			'relation' => 'OR',
			[
				'key'     => 'hp_hidden',
				'compare' => 'NOT EXISTS',
			],
			[
				'key'     => 'hp_hidden',
				'value'   => '1',
				'compare' => '!=',
			],
		];

		$query->set( 'meta_query', $meta_query );
	},
	1000
);

Ticking the checkbox now removes that listing from the Listings page, search results, and category archives, while its direct URL keeps working. Untick it to make the listing visible again. The NOT EXISTS clause is needed because HivePress deletes the meta entirely when a checkbox is unticked, rather than saving a “no” value.

And, to clarify; no – I’m not HivePress staff. I’m just a regular community member who enjoys helping out where I can. Helping solve other people’s problems has helped me learn about HivePress in much more detail, provide inspiration for my own site, and helps me increase my knowledge about HivePress should I run into similar issues down the line.

I also aim to inspire the community to be more involved and hopefully take some of the pressure off the HivePress team, so they can focus on implementing bug fixes and new features, etc.

I believe what Kseniia meant was that the help the HivePress staff provide falls under their support policy:

If you require further assistance in regard to developer guidance, custom implementation advice, and small code snippets, you should consider renewing your premium support, which you can do here:

For things outside either of these scopes, like custom themes, extensions or large logic changes, you can also consider hiring a HivePress-vetted expert here:

I hope this helps!

Cheers,
Chris :victory_hand:

3 Likes

Please try the code snippet suggested by Chris in the previous post. The snippet looks fine, and I would follow the same approach: add a custom Checkbox attribute (without code, in Listings → Attributes) and add a condition to the listing query (via the code snippet) to exclude listings with this attribute checked. This way, you can control which listings are “unlisted” on a per-listing basis.

Thanks for your feedback – we’ll consider adding a similar “unlist” option to future updates, in addition to the available “hide” option.

Hi @hamid,

Just another quick update to say that I’ve tested the latest Attribute checkbox approach, and it works on my end! :slight_smile:

Cheers,
Chris :victory_hand:

2 Likes

I remembered there’s a pretty recent hook available that targets the listing query (it can also be used for vendors, requests, etc), so this simplified snippet may also work:

add_action(
	'hivepress/v1/models/listing/query',
	function ( $query ) {
		$meta_query = (array) $query->get( 'meta_query' );

		$meta_query[] = [
			'key'     => 'hp_hidden',
			'compare' => 'NOT EXISTS',
		];

		$query->set( 'meta_query', $meta_query );
	},
	1000
);

Combined with a custom attribute of Checkbox type and with Field Name set to “hidden” in Listings/Attributes, it’s possible to hide any listing in the search results while keeping it accessible by URL.

1 Like

Hi dear ChrisB :slightly_smiling_face:

I also used and checked the latest code (based on a checkbox attribute) and it worked well!

Thanks a lot again for your help and support. :heart_eyes:

1 Like