Support

Account

Forum Replies Created

  • Thank you very much @matthewmcvickar, you were totally on it there! Turns out there’s two ways to do it.. the first is to turn off the auto-sort functionality in the plugin’s settings or you can use the ignore_custom_sort parameter in the query.

    'ignore_custom_sort' => true,
    'meta_key' => 'date', 
    'orderby' => 'meta_value_num',
    'order' => 'DESC'

    Thank you for your help.

  • AH HA! Yes, totally didn’t think about that.
    I have a plugin called Post Types Order running, it let’s the client drag and drop posts in the edit screen list to re-order. I just tested disabling it and that worked!

    However, I need this plugin running for the other post-types, any idea how to over-rule it for this instance?

  • Here, you can see from this screenshot that the output order from the WP_Query is totally unrelated to my date custom field (which is display as a date under each article).

  • Thanks for your response @matthewmcvickar.
    My date field is a custom field created in ACF, not the post’s publish date.

    So I tried a print_r() of the $news_query but I’m really not sure what I’m looking for. ( nor is my knowledge of mysql all that good ).
    I’m pasting the output here in the hope that you might spot the problem.

    WP_Query Object
    (
        [query] => Array
            (
                [post_type] => news
                [post_status] => publish
                [posts_per_page] => 12
                [meta_query] => Array
                    (
                        [relation] => AND
                        [0] => Array
                            (
                                [key] => archive
                                [value] => 1
                            )
    
                        [1] => Array
                            (
                                [key] => featured_article
                                [value] => 0
                            )
    
                    )
    
                [meta_key] => date
                [orderby] => meta_value_num
                [order] => DESC
            )
    
        [query_vars] => Array
            (
                [post_type] => news
                [post_status] => publish
                [posts_per_page] => 12
                [meta_query] => Array
                    (
                        [relation] => AND
                        [0] => Array
                            (
                                [key] => archive
                                [value] => 1
                            )
    
                        [1] => Array
                            (
                                [key] => featured_article
                                [value] => 0
                            )
    
                    )
    
                [meta_key] => date
                [orderby] => meta_value_num
                [order] => DESC
                [error] => 
                [m] => 
                [p] => 0
                [post_parent] => 
                [subpost] => 
                [subpost_id] => 
                [attachment] => 
                [attachment_id] => 0
                [name] => 
                [static] => 
                [pagename] => 
                [page_id] => 0
                [second] => 
                [minute] => 
                [hour] => 
                [day] => 0
                [monthnum] => 0
                [year] => 0
                [w] => 0
                [category_name] => 
                [tag] => 
                [cat] => 
                [tag_id] => 
                [author] => 
                [author_name] => 
                [feed] => 
                [tb] => 
                [paged] => 0
                [meta_value] => 
                [preview] => 
                [s] => 
                [sentence] => 
                [title] => 
                [fields] => 
                [menu_order] => 
                [embed] => 
                [ignore_sticky_posts] => 
                [suppress_filters] => 
                [cache_results] => 1
                [update_post_term_cache] => 1
                [lazy_load_term_meta] => 1
                [update_post_meta_cache] => 1
                [nopaging] => 
                [comments_per_page] => 50
                [no_found_rows] => 
            )
        [meta_query] => WP_Meta_Query Object
            (
                [queries] => Array
                    (
                        [0] => Array
                            (
                                [key] => date
                            )
    
                        [1] => Array
                            (
                                [0] => Array
                                    (
                                        [key] => archive
                                        [value] => 1
                                    )
    
                                [1] => Array
                                    (
                                        [key] => featured_article
                                        [value] => 0
                                    )
    
                                [relation] => AND
                            )
    
                        [relation] => AND
                    )
    
                [relation] => AND
                [meta_table] => wp_postmeta
                [meta_id_column] => post_id
                [primary_table] => wp_posts
                [primary_id_column] => ID
                [table_aliases:protected] => Array
                    (
                        [0] => wp_postmeta
                        [1] => mt1
                        [2] => mt2
                    )
    
                [clauses:protected] => Array
                    (
                        [wp_postmeta] => Array
                            (
                                [key] => date
                                [compare] => =
                                [alias] => wp_postmeta
                                [cast] => CHAR
                            )
    
                        [mt1] => Array
                            (
                                [key] => archive
                                [value] => 1
                                [compare] => =
                                [alias] => mt1
                                [cast] => CHAR
                            )
    
                        [mt2] => Array
                            (
                                [key] => featured_article
                                [value] => 0
                                [compare] => =
                                [alias] => mt2
                                [cast] => CHAR
                            )
    
                    )
    
                [has_or_relation:protected] => 
            )
    
        [date_query] => 
        [request] => SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )  INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id )  INNER JOIN wp_postmeta AS mt2 ON ( wp_posts.ID = mt2.post_id ) WHERE 1=1  AND ( 
      wp_postmeta.meta_key = 'date' 
      AND 
      ( 
        ( mt1.meta_key = 'archive' AND mt1.meta_value = '1' ) 
        AND 
        ( mt2.meta_key = 'featured_article' AND mt2.meta_value = '0' )
      )
    ) AND wp_posts.post_type = 'news' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order, wp_postmeta.meta_value+0 DESC LIMIT 0, 12
        [posts] => Array
            (
            ........
            )
    )
    

    I’ve edited some of the empty entries out of it because it was even longer.

  • Any idea why this would not be working for me? I’m going round in circles trying to figure it out, please could you tell me where I’m going wrong.

    $news_archive = ( int )( get_query_var('news_archive')  === 'true' );
    
    $news_query = new WP_Query( array(
        'post_type' => 'news',
        'post_status' => 'publish',
        'posts_per_page' => $news_archive ? 12 : 5,
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'archive',
                'value' => $news_archive,
                'compare' => '=',
            ),
            array(
                'key' => 'featured_article',
                'value' => 0,
            )
        ),
        'meta_key' => 'date', 
        'orderby' => 'meta_value_num',
        'order' => 'ASC'
    ));

    No change in the order of my posts.

    To begin with I thought that there might be restrictions on using the name date for my custom field.. but that doesn’t seem to be the case because I tested by adding a test_date field and trying it with that.

    I’ve also tried changing the output format of the date picker field, but that also doesn’t seem to affect anything because in newer versions of ACF date field values are stored as YYYYMMDD in the database regardless.

    Please help.

  • @zchmm Yea I agree..
    I don’t see how a private answer on a forum post is in any way useful to anyone. I’d say it actually went against the entire point of forum question and answer support.

  • Sorry for delay, so I was trying two things at once..

    • Meta_query without post_object field in a group
    • Meta_query on a post_object field that is not multi-select

    The first is obvious as you pointed out but is dependant on the second
    Am I correct in thinking that for the second I would need to omit the double quotes?
    'value' => $instrument,
    ..Instead of..
    'value' => '"'.$instrument.'"',

    What about compare? Should I use LIKE or IN for single post_object?

  • Yes! I just looked that up a few minutes ago after re-reading your last post on the other thread.. and tried exactly this. This was it! Thanks!

    Any idea why I get a warning error if I have the value in an array?
    'value' => array( '"'.$author.'"' ),

    I seem to need this if the post_object is not in a group.
    ( I know mine is but for future use I’d like to crack it all round )

  • Hi, please could someone advise me on whether I can treat an ACF group in the same way as a repeater? I’m trying to query a subfield of the group field in this way but I’m having no luck. I’ve got my my_posts_where functions setup but something is wrong.

    I’ve put my issue is on this forum thread..
    https://support.advancedcustomfields.com/forums/topic/wp_query-with-meta_query-on-a-subfield-within-a-group-field/

    Any help would be greatly appreciated.

  • @laconfiserie, thanks for your help and suggestions yesterday, I still not cracked it. I’ve moved my issue to a new forum thread because it’s really a separate issue to this one, please come over there to chip in if you have any thoughts and to continue this. Thanks again.
    https://support.advancedcustomfields.com/forums/topic/wp_query-with-meta_query-on-a-subfield-within-a-group-field/

  • @meandhim, thanks for your help, still not cracked it. I don’t know how to ‘look in authors_people’.. Do you mean look at the mySQL database? This is a bit over my head.

    I have moved my issue to a new forum thread, please come over there to chip in if you have any thoughts on this and to continue this. Thanks again.

    https://support.advancedcustomfields.com/forums/topic/wp_query-with-meta_query-on-a-subfield-within-a-group-field/

  • Ok, so I’ve narrowed it down to being a problem with my querying subfields inside a group field because I’ve tried the following:

    • created a test_people post_object field outside of the authors group
    • Keeping the return format as post object
    • Wrap the value in an array

    I’m able to get results with this code:

    $author = 85;
    $images_query = new WP_Query( array(
        'post_type' => 'images',
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'test_people',
                'value' => array( '"'.$author.'"' ),
                'compare' => 'LIKE',
            ),
        ),
    ));

    *wrapping the value in an array causes another problem but I want to solve the first:
    ( Warning: trim() expects parameter 1 to be string )

    The point it is, I’m not finding a way to query subfields with the function and this syntax:
    'key' => 'authors_%_people'

    Am I wrong in thinking groups can be treated like repeaters?

  • Ok, so I’ve narrowed it down to being a problem with my querying subfields inside a group field because I’ve tried the following:

    • created a test_people post_object field outside of the authors group
    • Keeping the return format as post object
    • Wrap the value in an array

    I’m able to get results with this code:

    $author = 85;
    $images_query = new WP_Query( array(
        'post_type' => 'images',
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'test_people',
                'value' => array( '"'.$author.'"' ),
                'compare' => 'LIKE',
            ),
        ),
    ));

    *wrapping the value in an array causes another problem but I want to solve the first:
    ( Warning: trim() expects parameter 1 to be string )

    The point it is, I’m not finding a way to query subfields with the function and this syntax:
    'key' => 'authors_%_people'

    Am I wrong in thinking groups can be treated like repeaters?

  • Let me know if you have any ideas, I’m going mad.

  • Let me know if you have any ideas, I’m going mad.

  • I am leaving a lot of stuff out because there’s way too much to paste here, but you get the idea.

    <?php
    $author = 85; //get_query_var('author');
    $images_query = new WP_Query([
        'suppress_filters' => FALSE,
        'posts_per_page' => -1,
        'post_type' => 'images',
        'meta_query' => [
            //'relation' => 'OR',
            //'relation' => 'AND',
            [
                'key' => 'authors_%_people',
                'compare' => 'LIKE',
                'value' => '"'.$author.'"',
            ],
        ],
    ]);
    ?>
    <div class="row grid">
    <?php
    if( $images_query->have_posts() ) :
    while( $images_query->have_posts() ) :
    $images_query->the_post();
    ?>
    <div id="<? the_ID() ?>" class="grid-item">
    <?= wp_get_attachment_image( get_field('image')['ID'] ?>
    </div>
    <?php
    endwhile;
    endif;
    wp_reset_query()
    ?>
    </div>
  • I cannot see where I’m going wrong.. Perhaps it’s relation in the meta_query ? Here is what I have now.

    In my function.php

    function my_posts_where( $where ) {
        global $wpdb;
        $where = str_replace(
            "meta_key = 'authors_",
            "meta_key LIKE 'authors_",
            $wpdb->remove_placeholder_escape( $where )
        );
        return $where;
    }
    add_filter( 'posts_where', 'my_posts_where' );

    And on my archive-images.php ….

    $author = 85; // This will eventuall be pulled from get_query_var('author')
    $images_query = new WP_Query([
        'suppress_filters' => FALSE,
        'posts_per_page' => -1,
        'post_type' => 'images',
        'meta_query' => [
            //'relation' => 'OR',
            //'relation' => 'AND',
            [
                'key' => 'authors_%_people',
                'compare' => 'LIKE',
                'value' => '"'.$author.'"',
            ],
        ],
    ]);

    What am I doing wrong?

  • Thanks. This is pretty much the same as I have. Although I did not have suppress_filters = FALSE in there, so I tried adding that. Still nothing. Do you think it could be something to do with the group field being different to the repeater?

  • Note :
    Actually my authors filed is not a repeater; its a group field but it’s basically the same as a repeater without the repeats.

  • That throws an error …
    Parse error: syntax error, unexpected '.85' (T_DNUMBER), expecting ']
    ( even after I added the missing comma after ‘LIKE’ ) What’s the ‘”‘.85.'” for?

  • I have now read section 4 of that article, but I don’t really understand it because I’m not too hot on mySQL. But either way, I’ve added the following to my functions file :

    function my_posts_where( $where ) {
        global $wpdb;
        $where = str_replace(
                  "meta_key = 'authors_", 
                  "meta_key LIKE 'authors_",
                  $wpdb->remove_placeholder_escape($where)
        );
        return $where;
    }
     
    add_filter('posts_where', 'my_posts_where');

    and I’m trying to query like this …

    $images_query = new WP_Query([
        'posts_per_page' => -1,
        'post_type' => 'images',
        'meta_query' => [
            [
                'key' => 'authors_%_people',
                'compare' => '='
                'value' => ['85'],
            ],
        ],
    ]);

    Any ideas why this wouldn’t be working? Could it be because I allow multiple values in the people post_object field?

    What do you mean by “create a custom field for this query”?

  • No, I didn’t realise that was needed. I have tried this now but still no luck. Is ‘compare’ => ‘IN’ correct? And is it correct to put the id for people in an array?

  • My fields are as follows: authors is a group field and within that I have a subfield of people (amongst other subfields). People is a multiple post_object select field.

    I have the impression from this thread that it is possible to query subfields in this way…

    $images_query = new WP_Query([
        'posts_per_page' => -1,
        'post_type' => 'images',
        'meta_query' => [
            [
                'key' => 'authors_%_people',
                'value' => ['85'],
                'compare' => 'IN'
            ],
        ],
    ]);

    It is not working, please could you advise me.

  • Please could you help me figure out how to run a WP_Query with meta_query such that posts-types are filtered by a subfield within a group field.

    I have a custom post type of image and an acf group field of authors with a subfield of people. People is a multi-select post_object field. What would I need to do to get images only where people contains a certain id?

    I’m trying something like this based on John Huebner’s comment above.

    $images_query = new WP_Query([
        'posts_per_page' => -1,
        'post_type' => 'images',
        'meta_query' => [
            [
                'key' => 'authors_%_people',
                'value' => ['85'],
                'compare' => 'IN'
            ],
        ],
    ]);

    I’ve clearly got something wrong, Nothing is working. Please help.

  • Oh dear. I just realised that my author field (multiple select post_object field) is a subfield of a group… How can I deal with this?

Viewing 25 posts - 1 through 25 (of 33 total)