WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#20079 closed defect (bug) (invalid)

wp_query excludes posts without custom field from search

Reported by: gdah.ayoub Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.3.1
Component: Query Keywords:
Focuses: Cc:

Description (last modified by SergeyBiryukov)

Merging these arguments with the query in search, excludes posts that do not have the custom field 'statut' from search.

$args = array(
	'posts_per_page' => $per_page,
	'orderby' => 'title',
	'order' => 'ASC',
	'post_type' => 'medicament',
	'post_status' => 'publish',
	'meta_query' => array(
                                        
		// exclude non commericalisés médicaments
		array(
			'key' => $prefix.'statut',
			'value' => 'Non commercialisé',
			'compare' => '!=',
			'type' => 'CHAR',
		),

	)
);

Change History (2)

comment:1 @SergeyBiryukov4 years ago

  • Description modified (diff)

comment:2 @scribu4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Yes, that's expected behavior. To also get posts that don't have the custom field at all, you'll have to write:

	'meta_query' => array(
        	'relation' => 'OR',
		array(
			'key' => $prefix.'statut',
			'value' => 'Non commercialisé',
			'compare' => '!=',
			'type' => 'CHAR',
		),
		array(
			'key' => $prefix.'statut',
			'compare' => 'NOT EXISTS',
		),
	)

Note that 'NOT EXISTS' is not implemented yet: #18158

Note: See TracTickets for help on using tickets.