Support Forum » User Profile

Forum Replies Created

Viewing 2 posts - 76 through 77 (of 77 total)
  • Author
    Posts
  • in reply to: pre_get_posts to get all posts connected to single object #11663
    Content PilotContent Pilot
    Participant
    
    function custom_query_vars_filter($vars) {
        $vars[] .= 'relationships';
        return $vars;
    }
    add_filter( 'query_vars', 'custom_query_vars_filter' );
    
    function prefix_get_related_posts( $query ) {
        
        $post_id = get_query_var('relationships');
        
        if ( empty( $post_id ) || is_admin() ) {
            return $query;
        }
    
        if( is_post_type_archive( 'poa_person' ) && $query->is_main_query() ) {
            $query->set( 'relationship', 
                array( 
                    'id' => 'poa_practice_to_poa_person',
                    'from' => $post_id,
                )
            );
        }
    
        return $query;
    }
    add_filter( 'parse_query', 'prefix_get_related_posts', 10, 1);
    

    parse_query works great. But now my styling is all jacked up. At least the query is working now. Thanks for the help.

    in reply to: Filter the orderby of entries retrieved from database #11270
    Content PilotContent Pilot
    Participant

    The function needs to be altered a little bit for anyone else looking to do the same thing. Only change was to remove ORDER BY in the new SQL statement.

    
    add_filter( 'posts_clauses', 'mb_sort', 99, 2 );
    /**
     * Sort just the POST relationship by date DESC.
     *
     * @param array $clauses Existing clauses.
     * @param WP_Query $query Main Query.
     * @return array
     */
    function mb_sort( $clauses, $query ) {
    
        $relationship = $query->get( 'relationship' );
        
        if ( ! $relationship || 'REL_ID' !== $relationship['id'] ) {
                return $clauses;
        }
        
        $clauses['orderby'] = 'post_date DESC';
        
        return $clauses;
        
    }
    
Viewing 2 posts - 76 through 77 (of 77 total)