Support » Theme: PHLOX » Adjust queries by pre_get_posts and get_adjacent_post via a plugin in Phlox

  • Hi I am trying to help a friend with the following scenario. They have a website for a local concert group using the latest WordPress install along with latest version of the Phlox Theme (2.3.0) and the Phlox Core Elements Plugin (2.3.0).

    They use the ‘post’ post type for their events. To be able to sort it in the front end based on the event date and time i’ve added the regular version of Advanced Custom Fields (5.7.7) and added a date and time field to the post post type. The condition is each event at the moment and newer should be shown in the event listing on the front page and sorted ascending.

    A few weeks back and a few earlier versions of Phlox I managed to get everything working in a small plugin. Meaning i’ve adjusted the $query with the aid of pre_get_posts so all events get properly displayed based on the conditions described before:

    function mv_pre_get_posts( $query ) {
    
    	if ( class_exists( 'ACF' ) ) {
    		// Queries aren't modified for the admin menu.
    		if ( is_admin() ) {
    			return $query;
    		}
    
    		// Queries are modified for the post type 'post' only.
    		if ( isset( $query->query_vars['post_type'] ) && ( 'post' === $query->query_vars['post_type'] || is_single() ) ) {
    			$query->set( 'orderby', 'meta_value' );
    			$query->set( 'meta_key', 'dattime' );
    			$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'     => 'dattime',
    					'compare' => '>=',
    					'value'   => date( 'Y-m-d H:i:s', strtotime( '-8 hours' ) ),
    					'type'    => 'DATETIME',
    				),
    			) );
    		}
    		return $query;
    
    	} else {
    		add_action( 'admin_notices', 'mv_mvquery_notice' );
    	}
    }
    add_action( 'pre_get_posts', 'mv_pre_get_posts' );

    that worked flawlessly but since a few versions it is completely broken. no sorting and no minding if the event already took place or if it is coming up.

    things got even worse in case there is a pagination. there are more pages shown as events available to fill those. and if i get to empty pages i get a not found. i remember when i debugged and got everything properly working i remember that there was a query related to the pagination. that one was missing at some point after an update to phlox. unfortunately i cant remember the queries name as well as at which phlox version things broke. anyway the functions i’ve used and which where also working properly are the following (i only paste the one for previous):

    function mv_adjacent_post_join() {
    	global $wpdb;
    	return "INNER JOIN $wpdb->postmeta AS mvpm ON mvpm.post_id = p.ID";
    }
    add_filter( 'get_previous_post_join', 'mv_adjacent_post_join' );
    
    function mv_previous_post_where() {
    	global $wpdb, $post;
    	$mv_event_constraint = date( 'Y-m-d H:i:s', strtotime( '-8 hours' ) );
    	$mv_current          = get_post_meta( $post->ID, 'dattime', true );
    	return $wpdb->prepare( " WHERE mvpm.meta_key = 'dattime' AND CAST(mvpm.meta_value AS DATETIME) >= %s AND mvpm.meta_value < %s AND p.post_type = 'post' AND p.post_status = 'publish'", $mv_event_constraint, $mv_current );
    }
    add_filter( 'get_previous_post_where', 'mv_previous_post_where' );
    
    function mv_previous_post_sort() {
    	return 'ORDER BY mvpm.meta_value DESC LIMIT 1';
    }
    add_filter( 'get_previous_post_sort', 'mv_previous_post_sort' );

    Has there anything changed with the phlox internals i was unable to find? i also checked the documentation looking for query related documents also no luck. any hints are appreciated. best regards r.

  • The topic ‘Adjust queries by pre_get_posts and get_adjacent_post via a plugin in Phlox’ is closed to new replies.