Support

Account

Home Forums General Issues Alter pagination of an existing query to go by a custom field

Solved

Alter pagination of an existing query to go by a custom field

  • I ran into a bit of an odd problem. So far I always built themes from the ground up. But at the moment I am helping friends migrating to WordPress with the website of their concert gourp. They have chosen to use Phlox (based on Elementor) to build their page on. They are using the standard “post” post type for adding events like concerts and parties and alike. In the beginning they used the publish date as the date of the event. But that wasn’t ideal. I suggested utilize ACF for that purpose ( the non pro version for the moment). I’ve appended one date + time field to the standard “post” post type. Phlox isn’t using the main query by the way! I’ve altered the specific query with pre_get_posts by the following code:

    
    if ( isset( $query->query_vars['post_type'] ) && 'post' === $query->query_vars['post_type'] ) {
    			$query->set( 'orderby', 'meta_value' );
    			$query->set( 'meta_key', 'even_start' );
    			$query->set( 'order', 'ASC' );
    			$query->set( 'meta_query', array(
    				'relation' => 'AND',
    				array(
    					'key'     => 'even_start',
    					'compare' => '>=',
    					'value'   => date( 'Y-m-d H:i:s', strtotime( '-8 hours' ) ),
    					'type'    => 'DATETIME',
    				),
    			) );
    		}
    		return $query;
    

    added that in the form of a plugin. That way only posts/events newer than today are shown. everything else is blended out. But my problem is that the pagination of the “post” post type still refers to the publish date and not the even_start field. Is there a way to alter to what the pagination function is referring to? and is it possible to alter it also in the scope of the plugin functionality? Thanks and best regards r.

  • …and on brief addition. It also happens that if you go on the previous post button that also events/post from the past are shown as well. more or less the entire query i sorted with the code above gets paginated. So more specifically the question would be is it possible to paginate ONLY the shown and sorted posts based in the event_start custom field.

    p.s. tried to update my initial post but somehow it seems impossible meanwhile at least i was unable to find an edit button. :/ there for sorry for the necessary follow up post.

  • Ok figured out the problem. In case anyone is dealing with similar issues:

    		if ( isset( $query->query_vars['post_type'] ) && ( 'post' === $query->query_vars['post_type'] || is_single() ) ) {
    			$query->set( 'orderby', 'meta_value' );
    			$query->set( 'meta_key', 'event_start' );
    			$query->set( 'order', 'ASC' );
    			$query->set( 'paged', get_query_var( 'paged' ) );
    			$query->set( 'posts_per_page', get_option( 'posts_per_page' ) );
    			$query->set( 'meta_query', array(
    				'relation' => 'AND',
    				array(
    					'key'     => 'event_start',
    					'compare' => '>=',
    					'value'   => date( 'Y-m-d H:i:s', strtotime( '-8 hours' ) ),
    					'type'    => 'DATETIME',
    				),
    			) );
    		}
    		return $query;

    Only paged and pages_per_page had to be added. Now it is working proplery.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.