Support » Fixing WordPress » Or Logic in Querying Posts by Custom Field

  • Is there any information about how to query by OR logic rather than AND logic?

    I’m trying to modify my author page’s main loop so that it also displays posts by a custom field, photo_credit, set to the author’s display name.

    This below example will need to be rewritten so that the logic is.

    AUTHOR = XYZ || meta key == photo credit && meta value == AUTHOR_NAME

    add_action('pre_get_posts', 'getPhotoCredits');
    function getPhotoCredits($query){
    		$curauth = get_user_by('slug', $query->query_vars['author_name']);
    		$query->query_vars['meta_key'] = 'photo_credit';
    		$query->query_vars['meta_value'] = $curauth->first_name.' '.$curauth->last_name;
Viewing 1 replies (of 1 total)
  • I think to exactly specify what I’m looking here is an option to set the relation of a main WP Query object like you can in a WP Meta Query. Does anything exist that fits that need? Below is an example of the syntax I’d expect.

    $curauth = get_user_by('slug', $query->query_vars['author_name']);
    $query->query_vars['relation'] = 'OR';
    $query->query_vars['meta_query'] = array(
    	'key' => 'photo_credit',
    	'value' => $curauth->first_name.' '.$curauth->last_name
Viewing 1 replies (of 1 total)
  • The topic ‘Or Logic in Querying Posts by Custom Field’ is closed to new replies.