WordPress.org

Ready to get started?Download WordPress

Forums

distinct meta_value on wp_query (4 posts)

  1. pettedemon
    Member
    Posted 1 year ago #

    hi,
    I can select the distinct value in my custom query?

    $filtra_categorie = array(
    	'cat' => '4',
    	'meta_key' => 'categoria',
    	'orderby' => 'meta_value',
    	'order'    => 'ASC',
    	'posts_distinct' => 'meta_key'
    
    );
  2. bcworkz
    Member
    Posted 1 year ago #

    wp_Query has a bunch of filters you can hook to modify the query before it is run, I'm sure you can slip in DISTINCT using one of them. You cannot do it from the main arguments array.

  3. pettedemon
    Member
    Posted 1 year ago #

    How can I apply the filter only for my select query?

  4. bcworkz
    Member
    Posted 1 year ago #

    Ah, good question. You recognized the filter fires for every single query, no matter for what purpose.

    Ideally, your filter function would check for some defining characteristic in the passed query object that makes your query unique amongst all others. Perhaps setting a global variable would help with this identification. Or your main process might be able to add the filter just before the query is defined, then remove it immediately afterwards. I've not done this last idea, so I cannot be sure it would work properly. It would be a powerful technique if it does work.

Topic Closed

This topic has been closed to new replies.

About this Topic