• ACF field filter query

    Read more

    Order by ACF field

    $the_query = new WP_Query(array(
        'post_type'         => 'event',
        'posts_per_page'    => -1,
        'meta_key'          => 'featured',
        'orderby'           => 'meta_value',
        'order'             => 'DESC'
    ));

    Filter by ACF field

    $posts = get_posts(array(
        'posts_per_page'    => -1,
        'post_type'     => 'post',
        'meta_query'    => array(
            'relation'      => 'AND',
            array(
                'key'       => 'color',
                'value'     => array('red', 'orange'),
                'compare'   => 'IN',
            ),
            array(
                'key'       => 'featured',
                'value'     => '1',
                'compare'   => '=',
            ),
        ),
    ));

    Sources:

    • https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
    • https://www.advancedcustomfields.com/resources/order-posts-by-custom-fields/
  • get_posts() vs WP_Query

    Read more

    $args are the same tho.

    get_posts is a function that will return an array, when WP_Query is a class which will be used with the loop related functions.

    Source:

    • https://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts#answer-1755
    • https://developer.wordpress.org/reference/classes/wp_query/#parameters
  • $_GET / query variables

    Read more

    To retrieve $_GET parameters in a secure way, one can use get_query_var

    get_query_var( string $query_var, mixed $default_value = '' ): mixed

    The parameters would have first to be added to the public query variables available to WP_Query.

    add_filter( 'query_vars', 'dev__pommes' );
    
    function dev__pomme( $qvars ) {
    	$qvars[] = 'pommes';
    	return $qvars;
    }

    Source : https://developer.wordpress.org/reference/functions/get_query_var/