Search code examples
phpwordpressadvanced-custom-fieldscustom-wordpress-pagesacfpro

not able to filter WordPress posts with custom meta-data


this is how I load the post via backend via PHP

$projects = new WP_Query([
      'post_type' => 'verlage',
      'posts_per_page' => -1,
      'order_by' => 'date',
      'order' => 'desc',
      'meta_key' => 'publiziert_in_sprache',
      'meta_value' => 'a:1:{i:0;s:7:\"Deutsch\";}'
    ]);

The post I want to load has the following data (got it via postman):

{
  "_cdp_origin": [
    "194"
  ],
  "_cdp_origin_site": [
    "-1"
  ],
  "_cdp_origin_title": [
    "I am a title"
  ],
  "_cdp_counter": [
    "65"
  ],
  "_thumbnail_id": [
    "24"
  ],
  "_edit_lock": [
    "1642265581:1"
  ],
  "_edit_last": [
    "1"
  ],

  "publiziert_in_sprache": [
    "a:1:{i:0;s:7:\"Deutsch\";}"
  ],
  "_publiziert_in_sprache": [
    "field_61be186edcb0b"
  ]
}

the post doesnt get displayed. its set to public so that should work. also if I remove the meta filter it gets displayed.


Solution

  • "publiziert_in_sprache": [
        "a:1:{i:0;s:7:\"Deutsch\";}"
    ],
    

    Note how this is using double quotes as string delimiters, so the escaped double quotes inside are actual double quotes, after the escaping has been resolved. The actual value you got there, is a:1:{i:0;s:7:"Deutsch";}

    'meta_value' => 'a:1:{i:0;s:7:\"Deutsch\";}'
    

    In your code, you used single quotes as string delimiters, so these \" now are not escaped double quotes any more; they simply are a backslash character, followed by a double quote character. The actual value you got here, is a:1:{i:0;s:7:\"Deutsch\";}

    Your query is looking for backslashes in the value, that aren't contained in what is actually stored in the database.

    So try with

    'meta_value' => 'a:1:{i:0;s:7:"Deutsch";}'
    

    instead.