Support

Account

Home Forums Front-end Issues Sorting / Querying from One Custom Post

Solved

Sorting / Querying from One Custom Post

  • Sorry for this thread. I am sure this is something fairly basic but Im not sure exactly what to call this or where to look in the documentation.

    I have a site in which I have created three Custom Post Types

    volunteers
    seasons
    show

    The Volunteer post type has its Title (Full name of person) and ACF fields for first and last name

    The Season system post type is a straight default custom post with no ACF files

    the show custom post type has the following ACF fields

    show_season (page link filtered by post type Season)
    opening night (date picker)
    cast (repeater field with subfields show_role (text)and actor (post object))
    crew (repeater field with subfields crew_position (select) and crew_person_name(post object))

    What I am trying to do (unsuccessfully) is modify my single-volunteer.php file so when I am looking a single volunteer’s page (their “bio” so to speak) I would see the following information queried, compiled and sorted as such

    ONSTAGE WORK:
    Show name (show_season) – show_role
    (the above would be sorted by show_season and then opening_night Descending)

    OFFSTAGE WORK:
    Show name (show_season) – crew_position

    I wasn’t sure what to search for in the docs / forums to try and point myself in the right direction so I can fish for myself. Any guidance is appreciated

  • Hi @okozarsky

    So the Actor post object field and crew_person_name post object field are filtered to volunteers?

  • This reply has been marked as private.
  • Hi @okozarsky

    So I think I get what you’re after. My question was to verify the setup you have.

    What you want to do is query all cpt shows based on their subfield values of the ID for the current volunteer post you’re at.

    Take a look at example 4 here: http://www.advancedcustomfields.com/resources/query-posts-custom-fields/

    You want your arguments to be something like:

    
    //Get the volunteer posts ID
    $post_id = get_the_ID();
    
    //Fetching all shows the volunteer was actor in
    $args = array(
    	'numberposts'	=> -1,
    	'post_type'		=> 'show',
    	'meta_query'	=> array(
    		array(
    			'key'		=> 'cast_%_actor',
    			'compare'	=> '=',
    			'value'		=> post_id,
    		)
    	)
    );
    
    //Fetching all shows the volunteer was crew in
    $args = array(
    	'numberposts'	=> -1,
    	'post_type'		=> 'show',
    	'meta_query'	=> array(
    		array(
    			'key'		=> 'crew_%_crew_person_name',
    			'compare'	=> '=',
    			'value'		=> post_id,
    		)
    	)
    );
    
  • just to be clear, for the values inside of ‘key’ => ‘——-‘

    I am putting in the exact name of my subfield name, not what you have, correct?

    should I be consider if the subfield in question Post Object or not?

  • If the repeater fields name is crew and the subfields name is crew_person_name you would put in
    crew_%_crew_person_name.

    Then you’d of course need to setup your posts_where filters to reflect this as well! What this does is basically that it looks through all values in post_meta where the meta_key is formatted like crew_number-of-row_crew_person_name (this is how ACF saves repeater field values).

    For a post object field the only thing that is actually saved to the DB is the post ID (the post object is fetches through the get_field/the_field functions) so you just need to query for the ID of the volunteer post.

  • I’ll try to tinker around with this and see where I get

  • This reply has been marked as private.
  • Well your code doesn’t do anything right not to actually fetch the info. 🙂

    You need to run it through a custom wp_query AND add the appropriate posts_where filter function as described in the documentation

    Take a look at example 4 here: http://www.advancedcustomfields.com/resources/query-posts-custom-fields/

    So your code needs to be more like:

    
    <?php
    //Get the volunteer posts ID
    $post_id = get_the_ID();
    
    //Fetching all shows the volunteer was actor in
    $args = array(
    	'numberposts'	=> -1,
    	'post_type'		=> 'show',
    	'meta_query'	=> array(
    		array(
    			'key'		=> 'cast_%_actor',
    			'compare'	=> '=',
    			'value'		=> $post_id,
    		)
    	)
    );
    
    $show_actor_query = new WP_Query($args);
    ?>
    
    <?php if( $show_actor_query->have_posts() ): ?>
    
    	<?php while( $show_actor_query->have_posts() ): $show_actor_query->the_post(); ?>
    
    		<h1><?php the_title(); ?></h1>
    		
    	<?php endwhile; wp_reset_postdata(); ?>
    
    <?php endif; ?>
    
    

    I can’t really write all the code for you because it’d be a bit over the top and I strongly believe in learning by doing 🙂 But I’ll be happy to guide you in what you need to do.

    Here you can read more about the WP_Query object
    http://codex.wordpress.org/Class_Reference/WP_Query

  • This reply has been marked as private.
  • Hi @okozarsky

    No problem glad to help out.

    You’re pretty much correct. The reason nothing is shown is due to the fact that the query does not find any posts (check the “foundposts” key in the wp_query object).

    Have you also set up the posts_where filter?
    Also, are you sure the volunteer you’re testing this on has shows connected?

  • This reply has been marked as private.
  • If you haven’t added the posts_where filter that is definitely why you’re not seeing any results.

    Please have a look at the link I’ve posted twice now. In there Elliot explains how to use the posts_where filter with example 🙂

    It does get added to your functions.php yes. The reason you need this filter is to modify WordPress core behaviour for meta_queries to do a LIKE search letting you find crew_0_blabla, crew_1_blabla etc.

  • This reply has been marked as private.
  • Great news 🙂

    You can close it yourself by setting an answer as the answer to your topic.

    May I change the visibility of your posts so that others can benefit from this topic as well? If you like I can remove all links to your website.

  • This reply has been marked as private.
  • This reply has been marked as private.
  • Hi,

    Could you please repost or edit so the code is within code tags 🙂

  • This reply has been marked as private.
  • This reply has been marked as private.
  • Hi,

    No you need to post your code within code tags. You can find the button for this in the toolbar above the textinput where you write an answer.

  • This reply has been marked as private.
  • This reply has been marked as private.
Viewing 25 posts - 1 through 25 (of 33 total)

The topic ‘Sorting / Querying from One Custom Post’ is closed to new replies.