Home › Forums › Pro Support › Filter by meta does not work
Tagged: "Meta key", "Meta value"
- This topic has 19 replies, 2 voices, and was last updated 2 years ago by
elvin.
-
AuthorPosts
-
September 6, 2021 at 2:48 pm #33524
Focolares
ParticipantHello,
I configured one of the WP Show Posts shortcodes with a filter based on the pair meta_key = “um_restriction_content” / meta_value=”um_interno”. Results are no posts. None other filters are actincg. As soon I remove the meta filter, the posts are displayed again. It seems the filter is not acting. I copied the names of meta_key and meta_value directly from the Wp database with phpMyadmin.
What I am doing wrong?
RegardsSeptember 6, 2021 at 11:22 pm #33538elvin
ModeratorHi there,
To clarify, are you trying to show posts with custom field
um_restriction_content
that has a field value ofum_interno
?Can you show us the code you’re using to check for any potential issues with it?
September 7, 2021 at 12:05 am #33542Focolares
ParticipantHi,
I enclose a couple of screenshoots which hopefully clarify.I do not use any code, just wpsp settings:
https://testsdii.focolars.cat/wp-content/uploads/2021/09/Screenshot-2021-09-06-23.38.08.pngThe meta field and value are taken from WP database:
https://testsdii.focolars.cat/wp-content/uploads/2021/09/Screenshot-2021-09-06-23.35.03.pngSeptember 7, 2021 at 12:10 am #33544elvin
ModeratorI can’t seem to access the links provided.
Can you try uploading them on sites like imgur?
or perhaps provide a Cloudapp or Loom screenshot?
September 7, 2021 at 12:52 am #33546Focolares
ParticipantAre you able to get access to this video now?
https://www.loom.com/share/9d1a460249d546088b58756a5cbfdf2cSeptember 7, 2021 at 6:40 pm #33574elvin
ModeratorLet’s dig a bit deeper and do some checks.
Can you do a var_dump() of the post’s post_meta?
Sample code:
$meta_values = get_post_meta( get_the_ID() ); var_dump( $meta_values );
Hook this on an existing hook on your theme’s single post page. This way we can see and fully verify the post meta in existence by going to a single post page with the supposed
um_restriction_content
meta.September 8, 2021 at 12:55 am #33586Focolares
ParticipantIn the single post with that restriction I got this dump:
array(18) { [“_edit_lock”]=> array(1) { [0]=> string(12) “1630963642:1” } [“classic-editor-remember”]=> array(1) { [0]=> string(12) “block-editor” } [“_edit_last”]=> array(1) { [0]=> string(1) “1” } [“um_content_restriction”]=> array(1) { [0]=> string(422) “a:9:{s:26:”_um_custom_access_settings”;b:1;s:14:”_um_accessible”;i:2;s:16:”_um_access_roles”;a:2:{s:13:”administrator”;s:1:”1″;s:10:”um_interno”;s:1:”1″;}s:28:”_um_access_hide_from_queries”;b:0;s:19:”_um_noaccess_action”;i:1;s:30:”_um_restrict_by_custom_message”;i:0;s:27:”_um_restrict_custom_message”;s:0:””;s:19:”_um_access_redirect”;i:0;s:23:”_um_access_redirect_url”;s:42:”https://testsdii.focolars.cat/wp-login.php”;}” } [“_stc_notifier_status”]=> array(1) { [0]=> string(4) “sent” } [“_stc_notifier_request”]=> array(1) { [0]=> string(0) “” } [“_thumbnail_id”]=> array(1) { [0]=> string(4) “1163” } [“_stc_notifier_sent_time”]=> array(1) { [0]=> string(19) “2021-08-12 23:32:27” } [“mec_location_id”]=> array(1) { [0]=> string(1) “0” } [“mec_additional_location_ids”]=> array(1) { [0]=> string(6) “a:0:{}” } [“mec_organizer_id”]=> array(1) { [0]=> string(1) “0” } [“mec_additional_organizer_ids”]=> array(1) { [0]=> string(6) “a:0:{}” } [“wppb-content-restrict-type”]=> array(1) { [0]=> string(7) “message” } [“wppb-content-restrict-user-role”]=> array(1) { [0]=> string(10) “um_interno” } [“wppb-content-restrict-user-status”]=> array(1) { [0]=> string(8) “loggedin” } [“wppb-content-restrict-custom-redirect-url”]=> array(1) { [0]=> string(0) “” } [“wppb-content-restrict-message-logged_out”]=> array(1) { [0]=> string(0) “” } [“wppb-content-restrict-message-logged_in”]=> array(1) { [0]=> string(0) “” } }
I-InternoSeptember 8, 2021 at 1:33 am #33590Focolares
ParticipantBy the way, I am using Generate Press Premium Theme.
September 8, 2021 at 6:51 pm #33606elvin
ModeratorCan you display the var_dump on the single post page itself? Since you’re using GeneratePress, you can hook the var_dump code on
generate_after_header
hook. And then link us to that page. Thanks. 😀It seems to me that there’s no
um_restriction_content
but there isum_content_restriction
. I think that’s the meta key you should use.September 9, 2021 at 1:17 am #33617Focolares
ParticipantYou are right. In my message as of Sept 6, I reversed the words ordering when writing the meta_key. The correct one is “um_content_restriction” as stated in the video I sent to you. I enclose a new one to clarify what I am dumping: https://www.loom.com/share/322fc944c62f4665aabdc212a0a0a990
and I paste the dump:
array(18) { [“_edit_lock”]=> array(1) { [0]=> string(12) “1630963642:1” } [“classic-editor-remember”]=> array(1) { [0]=> string(12) “block-editor” } [“_edit_last”]=> array(1) { [0]=> string(1) “1” } [“um_content_restriction”]=> array(1) { [0]=> string(422) “a:9:{s:26:”_um_custom_access_settings”;b:1;s:14:”_um_accessible”;i:2;s:16:”_um_access_roles”;a:2:{s:13:”administrator”;s:1:”1″;s:10:”um_interno”;s:1:”1″;}s:28:”_um_access_hide_from_queries”;b:0;s:19:”_um_noaccess_action”;i:1;s:30:”_um_restrict_by_custom_message”;i:0;s:27:”_um_restrict_custom_message”;s:0:””;s:19:”_um_access_redirect”;i:0;s:23:”_um_access_redirect_url”;s:42:”https://testsdii.focolars.cat/wp-login.php”;}” } [“_stc_notifier_status”]=> array(1) { [0]=> string(4) “sent” } [“_stc_notifier_request”]=> array(1) { [0]=> string(0) “” } [“_thumbnail_id”]=> array(1) { [0]=> string(4) “1163” } [“_stc_notifier_sent_time”]=> array(1) { [0]=> string(19) “2021-08-12 23:32:27” } [“mec_location_id”]=> array(1) { [0]=> string(1) “0” } [“mec_additional_location_ids”]=> array(1) { [0]=> string(6) “a:0:{}” } [“mec_organizer_id”]=> array(1) { [0]=> string(1) “0” } [“mec_additional_organizer_ids”]=> array(1) { [0]=> string(6) “a:0:{}” } [“wppb-content-restrict-type”]=> array(1) { [0]=> string(7) “message” } [“wppb-content-restrict-user-role”]=> array(1) { [0]=> string(10) “um_interno” } [“wppb-content-restrict-user-status”]=> array(1) { [0]=> string(8) “loggedin” } [“wppb-content-restrict-custom-redirect-url”]=> array(1) { [0]=> string(0) “” } [“wppb-content-restrict-message-logged_out”]=> array(1) { [0]=> string(0) “” } [“wppb-content-restrict-message-logged_in”]=> array(1) { [0]=> string(0) “” } }
September 9, 2021 at 2:02 am #33619elvin
ModeratorI think this is because the value of
um_content_restriction
is a string with multiple characters other thanum_interno
.Doing a simple
value => 'um_interno'
would return notice because there’s no plain stringum_interno
as value ofum_content_restriction
.That said, you may have to resort to achieving the desired query with a filter.
You’ll need to use
compare => 'LIKE'
so the query checking looks for theum_interno
within the value ofum_content_restriction
.To do the filter, you’ll have to modify the plugin’s code a bit to Tom’s instruction here –
https://wpshowposts.com/support/topic/can-i-exclude-with-a-custom-meta-field-from-a-list/#post-13344And then use this PHP snippet:
add_filter( "wp_show_posts_shortcode_args", function ($args, $settings) { if (1234 === (int) $settings["list_id"]) { $args['meta_query'] = array( 'relation' => 'AND', array( 'key' => 'um_content_restriction', 'compare' => 'EXISTS' ), array( 'key' => 'um_content_restriction', 'value' => 'um_interno', 'compare' => 'LIKE' ), ); } return $args; },10,2);
And then replace 1234 on this part of the code –
if (1234 === (int) $settings["list_id"])
– to the WPSP list ID you’re using.September 9, 2021 at 12:15 pm #33624Focolares
ParticipantThank you, however it is delivered a critical error on wordpress.
I inserted this piece of code just before the query (line 381) at wp-show-posts.php.
Anything else I can do?September 9, 2021 at 8:01 pm #33645elvin
ModeratorThe PHP snippet I’ve provided goes to a Code snippets plugin or a child theme’s functions.php. It shouldn’t be placed on the plugin’s code.
It’s separate from Tom’s instructions of modification on the plugin’s file. The plugin code modification was to make sure my snippet would work. 🙂
September 10, 2021 at 1:12 am #33649Focolares
ParticipantWhen placing the above code in the child theme’s functions.php, I get this error message from WP admin:
Los cambios en tu código PHP se han revertido debido a un error en la lÃnea 196 del archivo wp-content/themes/generatepress_child/functions.php. Por favor, arréglalo y trata de guardar de nuevo.
Uncaught ArgumentCountError: Too few arguments to function {closure}(), 1 passed in wp-includes/class-wp-hook.php on line 292 and exactly 2 expected in wp-content/themes/generatepress_child/functions.php:196
Stack trace:
#0 wp-includes/class-wp-hook.php(292): {closure}()
#1 wp-includes/plugin.php(212): WP_Hook->apply_filters()
#2 wp-content/plugins/wp-show-posts/wp-show-posts.php(383): apply_filters()
#3 wp-content/plugins/wp-show-posts/wp-show-posts.php(569): wpsp_display()
#4 wp-includes/shortcodes.php(343): wpsp_shortcode_function()
#5 [internal function]: do_shortcode_tag()
#6 wp-includes/shortcodes.php(218): preg_replace_callback()
#7September 10, 2021 at 3:27 pm #33665Focolares
ParticipantSorry for not explaining myself: linea 196 is just the line number in functions.php file (child theme) where I placed your code.
-
AuthorPosts
- You must be logged in to reply to this topic.