Returning CPT meta array, if not empty
- This topic has 6 replies, 2 voices, and was last updated 8 years, 11 months ago by
jonwatson87.
-
AuthorPosts
-
June 4, 2016 at 11:33 PM #3294
jonwatson87
ParticipantI've been battling this one for hours and can't quite figure it out.
What I want to be able to do is this:
- Get the meta data for the following post types: management, board and fellows
- Return the value of each radix_authoraccount, if it exists, as an array
- Convert that array using array_unique in order to filter out duplicate values
- 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?
June 5, 2016 at 1:51 AM #3297jonwatson87
ParticipantSo, 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);
June 5, 2016 at 3:15 AM #3298jonwatson87
ParticipantLatest 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...
June 5, 2016 at 4:38 AM #3300jonwatson87
ParticipantGOT 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!
June 5, 2016 at 6:29 AM #3301jonwatson87
ParticipantFull 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 { }
June 5, 2016 at 11:16 AM #3303Anh Tran
KeymasterHi @jonwatson87,
I'm very happy to see you figured it out. The way you use the helper function is correct!
June 5, 2016 at 8:55 PM #3318jonwatson87
ParticipantAwesome! 😀
Thanks, Anh!
-
AuthorPosts
- The topic ‘Returning CPT meta array, if not empty’ is closed to new replies.