Returning CPT meta array, if not empty

Support General Returning CPT meta array, if not empty

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #3294
    jonwatson87jonwatson87
    Participant

    I've been battling this one for hours and can't quite figure it out.

    What I want to be able to do is this:

    1. Get the meta data for the following post types: management, board and fellows
    2. Return the value of each radix_authoraccount, if it exists, as an array
    3. Convert that array using array_unique in order to filter out duplicate values
    4. Use that array in an author query to return posts by management, board and fellows who have associated author accounts (assigned via the below code)
    //Is post author?
    
    	$meta_boxes[] = array(
    		'id'         => 'tm_post_author',
    		'title'      => 'Post Author?',
            'post_types' => array('board','fellows','management'),
    		'context'    => 'normal',
    		'priority'   => 'low',
    		'fields' 	 => array(
    			array(
    				'name' => 'Is this team member an opinion or work author?',
    				'desc'  => 'Yes',
    				'id'   => $prefix . 'authorcheckbox',
    				'type' => 'checkbox',
    				// Value can be 0 or 1
    				'std'  => 0,
    			),
    		)
    	);
    
    	//Author ID
    
    	$meta_boxes[] = array(
    		'id'         => 'author_account',
    		'title'      => 'Associated Author Account',
            'post_types' => array('board','fellows','management'),
    		'context'    => 'normal',
    		'priority'   => 'low',
    		'visible' => array('radix_authorcheckbox', true),
    		'fields' 	 => array(
    			array(
    				'name'  => 'Author Account',
    				'id'    => $prefix . 'authoraccount',
    				'type'  => 'user',
    			),
    		)
    	);

    At the moment I'm struggling with step 2.

    I can get the values as a string, using a custom query or foreach, but not an array, which I need to do in order to use array_unique.

    I'm probably missing something simple, but I just can't get it to work properly. Any suggestions for the best way to go about this?

    #3297
    jonwatson87jonwatson87
    Participant

    So, I've got the array and sorting to work, but not with radix_authoraccount... if I change 'radix_authoraccount' to a standard post field, like 'post_title', and 'SORT_NUMERIC' to 'SORT_REGULAR' (because title is no longer a numeric value) it works perfectly - printing the array of values minus the two duplicates.

    Any idea how I can get this to work with 'radix_authoraccount'?

    // Return an array of radix_authoraccount, if not empty, for management, board and fellows post types
    
    	$teamargs = array(
    		'post_type' => array('management','fellows','board'),
    	);
    
    	$team_posts = wp_get_recent_posts( $teamargs );
    
    	$teamauthorlist = array();
    
    	foreach( $team_posts as $teamauthors ) {
    
    		if ( !empty( array($teamauthors['radix_authoraccount']) ) ) {
    
    			$teamauthorlist[] = array($teamauthors['radix_authoraccount']);
    
    		}
    
    	}
    
    // Sort array to remove duplicate authors
    
    	$authorarray = array_unique($teamauthorlist, SORT_NUMERIC);
    
    	$theauthors = array_values( $authorarray );
    
    	print_r($theauthors);
    #3298
    jonwatson87jonwatson87
    Participant

    Latest update:

    I got it returning SOME values, by using:

    // Return an array of radix_authoraccount, if not empty, for management, board and fellows post types
    
    	$teamargs = array(
    		'post_type' => array('management','fellows','board'),
    	);
    
    	$team_posts = wp_get_recent_posts( $teamargs );
    
    	$teamauthorlist = array();
    
    		foreach( $team_posts as $teamauthors ) {  
    
    	 		if ( !empty( array(rwmb_meta( 'radix_authoraccount', '', $teamauthors['ID']) ) ) ) {
    
    	 			$teamauthorlist[] = array(rwmb_meta( 'radix_authoraccount', '', $teamauthors['ID']));
    
    	 		}
    
    	 	}
    
    // Sort array to remove duplicate authors
    
    print_r($teamauthorlist);
    
    $authorarray = array_unique($teamauthorlist, SORT_REGULAR);
    
    $theauthors = array_values( $authorarray );
    
    // print_r($theauthors);'

    But the values returned aren't quite right... According to the user data I can see, and the users assigned using radix_authoraccount, I should be getting:

    234455678

    Instead, the above code is returning:

    2333445578

    That's two extra 3s and a missing 6... Getting closer, but something's not quite right...

    #3300
    jonwatson87jonwatson87
    Participant

    GOT IT!

    Seems that the "user" field has a value even if it isn't selected, hence my need to put in a checkbox to begin with.

    I changed the if statement to the checkbox value and returned radix_authoraccount values based on that, instead.

    Final, working code:

    // Return an array of radix_authoraccount, if not empty, for management, board and fellows post types
    
    	$teamargs = array(
    		'post_type' => array('management','fellows','board'),
    		'posts_per_page' => -1,
    	);
    
    	$team_posts = wp_get_recent_posts( $teamargs );
    
    	$teamauthorlist = array();
    
    		foreach( $team_posts as $teamauthors ) { 
    
    	 		if (rwmb_meta( 'radix_authorcheckbox', '', $teamauthors['ID']) == 1 ) {
    
    	 			$teamauthorlist[] = array(rwmb_meta( 'radix_authoraccount', '', $teamauthors['ID']));
    
    	 		}
    
    	 	}
    
    // Sort array to remove duplicate authors
    
    	// print_r($teamauthorlist);
    
    	$authorarray = array_unique($teamauthorlist, SORT_REGULAR);
    
    	$theauthors = array_values( $authorarray );
    
    	print_r($theauthors);

    Thanks for being my sounding board!

    #3301
    jonwatson87jonwatson87
    Participant

    Full code, if it helps anyone in future (note that I had to adapt it slightly for use with Co-Authors Plus, so it echoes user_login with the prefix cap- and then uses that in the tax_query):

    // Return an array of radix_authoraccount, if not empty, for management, board and fellows post types
    
    	$teamargs = array(
    		'post_type' => array('management','fellows','board'),
    		'posts_per_page' => -1,
    	);
    
    	$team_posts = wp_get_recent_posts( $teamargs );
    
    	$theteamauthors = array();
    
    		foreach( $team_posts as $teamauthors ) { 
    
    	 		if (rwmb_meta( 'radix_authorcheckbox', '', $teamauthors['ID']) == 1 ) {
    
    	 			$teamauthorlist = rwmb_meta( 'radix_authoraccount', '', $teamauthors['ID']);
    
    				$getpostauthor = get_user_by( 'id', $teamauthorlist );
    				$authorlogin = $getpostauthor->user_login;
    
    	 			$theteamauthors[] = array('cap-' . $authorlogin);
    
    	 		}
    
    	 	}
    
    // Sort array to remove duplicate authors
    
    	// print_r($teamauthorlist);
    
    	$authorarray = array_unique($theteamauthors, SORT_REGULAR);
    
    	$authormap = array_map('array_pop', $authorarray);
    
    	$theauthors = implode(",",$authormap);
    
    	// echo $theauthors;
    	
    // Run query to retrieve active posts by these authors
    
    	$args = array(
    	    'numberposts' => '10',
    	    'orderby' => 'post_date',
    	    'order' => 'DESC',
    	    'post_type' => array( 'post', 'work', 'news' ),
    	    'meta_query'     => array(
    			array(
    				'key'     => 'radix_status',
    				'value'   => 'value1',
    				'compare' => '==',
    				)
    		),
    		'tax_query' => array(
    			array(
    				'taxonomy' => 'author',
    				'field' => 'slug',
    				'terms' => explode(',', $theauthors)
    			)
    		)
    	);
    
    	$recent_posts = new WP_Query ($args);
    
    	if ( $recent_posts->have_posts() ) {
    
    		echo '<div class="teamworklist"><ul>';
    
    		while ( $recent_posts->have_posts() ) {
    			$recent_posts->the_post();
    
    			// Post Title
    
    			$title = apply_filters( 'genesis_post_title_text', get_the_title() );
    			echo '<li>';
    			echo sprintf( '<h1 class="team-work entry-title"><a href="%s" rel="bookmark">%s</a></h1>', get_permalink(), $title );
    
    			// Post Date
    
    			echo get_the_date();
    			echo '<br>';
    
    			// Authors
    
    			echo 'By ';
    
    			coauthors_posts_links();
    
    			echo '</li>';
    
    		}
    
    		echo '</ul></div>';
    
    	} else {
    
    	}
    #3303
    Anh TranAnh Tran
    Keymaster

    Hi @jonwatson87,

    I'm very happy to see you figured it out. The way you use the helper function is correct!

    #3318
    jonwatson87jonwatson87
    Participant

    Awesome! 😀

    Thanks, Anh!

Viewing 7 posts - 1 through 7 (of 7 total)
  • The topic ‘Returning CPT meta array, if not empty’ is closed to new replies.