Support

Account

Home Forums Backend Issues (wp-admin) Cannot Query By Custom Field

Solving

Cannot Query By Custom Field

  • I made a flag on my woocommerce orders called “recurring” which flags whether an order recurs or not. When I select the flag on the Checkout page, the acf fields are being saved as encrypted fields I think because in the “Custom Fields” section of the view orders, it has new fields which are labeled “field_##UID##”.

    So, when i query wordpress

    
    	$args = array(
    	  'post_type' => 'shop_order',
    	 'post_status' => 'any',
    	  'posts_per_page' => -1,
    		'numberposts' => -1,
    	'meta_key'		=> 'recurring',
    	'meta_value'	=> 1
    	);
    
    // query
    $posts = get_posts($args);

    It doesnt return any posts, even though I know that the post is receiving the properties because it shows up on the order details.

    How do I query wordpress for shop orders which have this flag == 1?

  • get_posts() only gets post of 'post_type' => 'post'

    Use WP_Query https://codex.wordpress.org/Class_Reference/WP_Query

  • I’ve tried the same procedure using WP_QUERY and it still does not work. It returns an array with 0 post items even though i know there are at least 6 of them which it should be returning

  • This is the return value

    (
        [query] => Array
            (
                [post_type] => shop_order
                [post_status] => any
                [posts_per_page] => -1
                [numberposts] => -1
                [meta_query] => Array
                    (
                        [0] => Array
                            (
                                [key] => recurring
                                [value] => 1
                            )
    
                    )
    
            )
    
    ...
    
        [post_count] => 0
        [current_post] => -1
        [in_the_loop] => 
        [comment_count] => 0
        [current_comment] => -1
        [found_posts] => 0
        [max_num_pages] => 0
        [max_num_comment_pages] => 0
        [is_single] => 
        [is_preview] => 
        [is_page] => 
        [is_archive] => 
        [is_date] => 
        [is_year] => 
        [is_month] => 
    ...
    
    
  • 
    $args = array(
    	  'post_type' => 'shop_order',
    	 'post_status' => 'any',
    	  'posts_per_page' => -1,
    	'meta_key'		=> 'recurring',
    	'meta_value'	=> 1
    	);
    
    // query
    $recurring= new WP_Query($args);
    if ($recurring->have_posts()) {
      $recurring->the_post();
    
      // code to show what you want from these posts
    
      wp_reset_postdata();
    }
    

    see the WP_Query examples here https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

  • I’ve done this already, as explained above. I’ve even tried passing the query into a while loop using:
    <?php while( $posts->have_posts() ) : $posts->the_post(); ?>
    And still nothing.

    It doesnt seem to be retrieving any posts because of my above stated reasons

    • Fields are applying to my orders as “field_<some-key>”
    • Field value is hashed

    Why is this so different from the documentation. No where can i find any example of this in the docs or in a google search.

  • By the way, to clarify, I am not retriieving any posts when i perform a query via WP_Query and get_posts. There simply is nothing being returned.

    When i query using the field name as field_<key> with the hashed value that I’m seeing on my “View Order”, all the orders come in as expected.

    However, this is not a solution, because there are other field values that I am trying to retrieve, and they are also hashed, to values which are incomprehensible to me.

  • Here is a screenshot of what the fields look like on my order

  • When ACF saves a field there are two entries created in the database. One has a meta key of your field name with the value to be stored. The second has a meta key = "_{$your_field_name}" that has a value that is the ACF field key. Not sure if this is what you’re referring to.

    Exactly what kind of field is this field you’re referring to as a flag? Is it a true false field? or some other type of field?

    What do you mean when you say the value is a hashed value? Can you give me a better example? copy & past of the actual value stored?

  • I haven’t got a clue why you’re seeing that or why you need to look at the standard WP custom field meta box to see the custom fields? Where is your ACF field group added and how are the values getting into the ACF field if not through an ACF field group or ACF form?

  • When I refer to the meta key, I’m saying that it is being stored as field_<key> example being field_5919dea96926e. I can query by this key. I also know that this key, refers to my my ACF form field, because when i “field Keys” visible in my ACF screen options, it matches the key that I am trying to query. However, both the value and the field name are hashed.

    The field type that “recurring” is using is a “True/False” field type.

    The custom field is displaying normally in my order (view attachment) but is also appearing in my custom fields location.

    My field group is added on my checkout form, below the pricing details (view attachment 2)

  • Is it possible for you to look in your database? Look in your _postmeta table for the entry for this post ID, and meta key of your field name recurring (not field key) to see what it there? Is there anything?

  • In my DB in the _postmeta there is no fieldname named recurring or _recurring. I see whats in the attached image.

  • For that matter, there doesnt seem to be any of the field groups or field names in this section. I’ve made a sample field group to see if I could see that in the db, but it doesnt seem to be inputting the fields into the db

  • Before you can do a query based on the field first you need to figure out why the field is being added to the DB incorrectly, and/or where the right value is being saved.

    …and I don’t even know what questions to ask or where to begin to figure this out. What ecommerce plugin are you using?

  • woocommerce. I’ve also bought/downloaded ACF for woocommerce to integrate. But i cant seem to figure it out.

  • I have honestly never seen anything like this. Do you have this other plugin, ACF for WC, active. It says it encrypts fields, maybe that’s what’s causing the odd values.

  • Yes, that plugin is indeed activated. I see that it says “data encryption” in the plugin’s details, but I’m not sure how/why this is happening, or how to change the setting.

    I suppose that isnt your problem, however, I dont know what else to do. The plugin developer for that particular plugin has yet to respond to me.

Viewing 18 posts - 1 through 18 (of 18 total)

The topic ‘Cannot Query By Custom Field’ is closed to new replies.