Or Logic in Querying Posts by Custom Field (2 posts)

  1. JohnRI
    Posted 2 years ago #

    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;
  2. JohnRI
    Posted 2 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic