How can I limit the listings page based on meta data?

I have created a template in the wp-admin to replace the default listings page. It eliminates the sidebar and has the search form at the top, the listings in 3 colums, then pagination. Right now it shows all listings. I have an attribute called “auction_start_date” another called “auction_end_date” I would like to pass in a query variable (eg. ?t=past or ?t=current) and have the listing pages show only all listings where the end_date is before today for ?t=past OR show only all listings where the end_date is equal to or greater than toady for ?t=current.

If I go into plugins/hivepress/includes/blocks/class-listings.php and put a var_dump($listing) after $listing = Models\Listing::query()->get_by_id( get_post() ) I get a large dump of data which includes the data dump below. However, var_dump($listing->auction_start_date) returns NULL. If I can just get that value of “2023-11-06 09:00:00” then I’ll be done. How do I get that date so that I can make a conditional statement on it?

Thanks.

[“auction_start_date”]=>

object(HivePress\Fields\Date)#2510 (27) {
[“args”:protected]=> array(18) {
[“display_type”]=> string(4) “date”
[“display_template”]=> string(21) “Auction date: %value%”
[“label”]=> string(18) “Auction Start Date”
[“type”]=> string(4) “date”
[“_order”]=> int(101)
[“required”]=> bool(true)
[“description”]=> NULL
[“placeholder”]=> NULL
[“min_date”]=> NULL
[“max_date”]=> NULL
[“offset”]=> NULL
[“time”]=> bool(true)
[“_display_areas”]=> array(3) {
[0]=> string(18) “view_block_primary” [1]=> string(17) “view_page_primary” [2]=> string(19) “view_page_secondary”
}
[“context”]=> array(2) {
[“listing”]=> RECURSION
[“model”]=> string(7) “listing”
}

     ["_indexable"]=> bool(true) 
     ["_external"]=> bool(true) 
     ["_alias"]=> string(21) "hp_auction_start_date" 
     ["name"]=> string(18) "auction_start_date" 
  } 

  ["display_type":protected]=> string(4) "date" 
  ["display_template":protected]=> string(21) "Auction date: %value%" 
  ["name":protected]=> string(18) "auction_start_date" 
  ["label":protected]=> string(18) "Auction Start Date" 
  ["description":protected]=> NULL 
  ["statuses":protected]=> array(0) { } 

  ["value":protected]=> string(19) "2023-11-06 09:00:00" 
  ["parent_value":protected]=> NULL 
  ["filter":protected]=> array(4) { 
     ["name"]=> string(18) "auction_start_date" 
     ["type"]=> string(4) "DATE" 
     ["value"]=> string(19) "2023-11-06 09:00:00" 
     ["operator"]=> string(1) "=" 
  } 

  ["disabled":protected]=> bool(false) 
  ["required":protected]=> bool(true) 
  ["errors":protected]=> array(0) { } 

  ["attributes":protected]=> array(5) { 
     ["data-format"]=> string(11) "Y-m-d H:i:s" 
     ["data-display-format"]=> string(12) "F j, Y g:i a" 
     ["data-time"]=> string(4) "true" 
     ["data-component"]=> string(4) "date" 
     ["class"]=> array(2) { 
        [0]=> string(8) "hp-field" [1]=> string(14) "hp-field--date" 
     } 
  } 

  ["context":protected]=> array(2) { 
     ["listing"]=> *RECURSION* 
     ["model"]=> string(7) "listing" 
  } 

  ["placeholder":protected]=> NULL 
  ["format":protected]=> string(11) "Y-m-d H:i:s" 
  ["display_format":protected]=> string(12) "F j, Y g:i a" 
  ["min_date":protected]=> NULL 
  ["max_date":protected]=> NULL 
  ["enabled_dates":protected]=> array(0) { } 

  ["disabled_dates":protected]=> array(0) { } 

  ["disabled_days":protected]=> array(0) { } 

  ["offset":protected]=> NULL 
  ["window":protected]=> NULL 
  ["multiple":protected]=> bool(false) 
  ["time":protected]=> bool(true) 

}

https://example.com/listings/?t=past would show only listings with the attribute “end_date” before today.

https://qa.furrow.com/listings/?t=current would show only listings with the attribute “end_date” equal to today or in the future.

I’ve been all through the code and scouring the dev doc Getting started - Developer Docs but I’m not having much success.

Any suggestions?

Thank you.

Thank you for waiting. Unfortunately, there’s no simple code snippet for this, it would require a custom implementation. It is needed to make changes with pre_get_posts | Hook | WordPress Developer Resources but please note that it can require further customization. If you are not familiar with the code customization, then please consider hiring someone for custom work https://fvrr.co/32e7LvY

I got it figured out. I’ll document it in a bit.

I was wrong. I tried following a pre_get_posts example found in this forum but it causes the listings page to be blank. Adding the code below to the theme’s functions.php makes class-listings.php’s render() function never fire. What could I be doing wrong?

add_action(
	'pre_get_posts',
	function($query){
		if(!is_admin() && 'hp_listing' === $query->get('post_type') && 'listings_view_page' === hivepress()->router->get_current_route_name()) {
			$query->set('orderby', 'asc');
		}
	},
	1000
);

This example is documented here: Change order of listings to random

Thank you for waiting. Please try this PHP code snippet to start. But it can require further customization. If you are not familiar with the code customization then please consider hiring someone for custom work Fiverr - Freelance Services Marketplace

add_action(
	'pre_get_posts',
	function($query){
		if ( is_front_page() || is_admin() || ! hp\get_array_value( $query->query, 'paged', true ) || $query->get( 'author' ) || is_single() || 'hp_listing' !== $query->get( 'post_type' ) || !hp\get_array_value( $_GET, 'put your meta data name here' ) ) {
			return;
		}

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

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