WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. JohnRI
    Member
    Posted 4 months 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){
    	if($query->is_author()){
    		$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
    Member
    Posted 3 months 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
    );

Reply

You must log in to post.

About this Topic