• Hi,

    I’m trying to learn how to filter custom posts on a custom field.
    I have an events post type that I’m using for a list of gigs.
    The event list will contain private posts for the bands eyes only.

    On the public facing gig list page I’m trying to filter by the ‘Gig Date’ (excluding all other types of the custom field ‘Type’ and also order them by the custom field ‘Date’.

    This is what I’ve got so far…

      	$gigsPosts = new WP_Query(array(
    	'post_type' => 'events',
    		'meta_key'		=> 'type',
    	'meta_value'	=> 'gig date',
    	'posts_per_page' => 10,
    	Array(
    	'meta_key' => 'date',	
    	'orderby' => 'meta_value',
    	'order' => 'DESC'	)
    		
    	)
     				  
    		);

    I’ve had varying results – none of them the right one!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The documentation on WP_Query has a few good examples that I suggest looking over.

    I’m not sure what you mean by “excluding all other types”. Do you mean excluding other post types? Usually meta keys don’t have spaces in them. You’ll need to figure out the real meta key name somehow. An example of a meta query could look like this:

    $gigsPosts = new WP_Query( array(
    	'post_type' => 'events',
    	'meta_key' => 'gig_date',
    	'orderby' => array(
    		'meta_value' => 'DESC',
    	),
    	'posts_per_page' => 10,
    ) );

    Are you using a plugin to create custom fields or are you manually creating custom fields?

    Moderator bcworkz

    (@bcworkz)

    Adding to what Howdy_McGee already said, you probably need to order by 'meta_value_num' instead of 'meta_value'. Numeric sorting is slightly different than alphabetic sorting. Your date fields need to be in yyyy-mm-dd style format or as a Unix timestamp. Month or day first formats will not sort properly.

    Thread Starter Dazzathedrummer

    (@dazzathedrummer)

    @howdy_mcgee what I mean by excluding other types is…

    The band can enter three or four different types of event post (Advanced Custom Field – ‘Type’ field).
    So for instance any band member can enter a ‘Gig date’, an ‘Unavailable date’, a ‘Rehearsal date’ and an ‘Other date’ so we can create a calendar of events for our eyes only on a private page for logged in band members.

    On the front end I only want to show the ‘Gig date’ entries out of the same list.

    So, in all, its a custom post type of ‘Events’ filtered to only show ‘Gig date’ (ACF custom field) and ordered by ACF ‘Date’ field.

    I’ve done this before using pure HTML/CSS and MySQL tables but I’m trying to learn how to do this purely in WP so I can make use of functionality that’s already there.

    Moderator bcworkz

    (@bcworkz)

    Our collective comments should work for you. Be sure that “gig_date” is the ACF field name used (not field label). You are presumably using the date picker field type. The display format can be whatever you like, but the return format should be Ymd, Y/m/d, Y-m-d, etc.

    Basic gig output for query results which uses ACF field’s display format:

    while ( $gigsPosts->have_posts() ) {
        $gigsPosts->the_post();
        $date_array = get_field_object('gig_date');
        $date = date( $date_array['display_format'], strtotime( $date_array['value']));
        echo '<li>' . get_the_title() . " - $date</li>";
    }

    The usual pagination functions found on theme templates will not work with custom queries. You should use paginate_links() for pagination links with custom queries. Your query will need slight modification to work with paginate_links(). See the User Notes near the bottom of the docs page for examples.
    https://developer.wordpress.org/reference/functions/paginate_links/

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Trying to learn about filtering custom fields’ is closed to new replies.