[Resolved] Filter by meta does not work

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Filter by meta does not work

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #33524
    Focolares
    Participant

    Hello,
    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?
    Regards

    #33538
    elvin
    Moderator

    Hi there,

    To clarify, are you trying to show posts with custom field um_restriction_content that has a field value of um_interno?

    Can you show us the code you’re using to check for any potential issues with it?

    #33542
    Focolares
    Participant

    Hi,
    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.png

    The 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.png

    #33544
    elvin
    Moderator

    I 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?

    #33546
    Focolares
    Participant

    Are you able to get access to this video now?
    https://www.loom.com/share/9d1a460249d546088b58756a5cbfdf2c

    #33574
    elvin
    Moderator

    Let’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.

    #33586
    Focolares
    Participant

    In 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-Interno

    #33590
    Focolares
    Participant

    By the way, I am using Generate Press Premium Theme.

    #33606
    elvin
    Moderator

    Can 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 is um_content_restriction. I think that’s the meta key you should use.

    • This reply was modified 1 week ago by elvin.
    #33617
    Focolares
    Participant

    You 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) “” } }

    #33619
    elvin
    Moderator

    I think this is because the value of um_content_restriction is a string with multiple characters other than um_interno.

    Doing a simple value => 'um_interno' would return notice because there’s no plain string um_interno as value of um_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 the um_interno within the value of um_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-13344

    And 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.

    #33624
    Focolares
    Participant

    Thank 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?

    #33645
    elvin
    Moderator

    The 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. 🙂

    #33649
    Focolares
    Participant

    When 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()
    #7

    #33665
    Focolares
    Participant

    Sorry for not explaining myself: linea 196 is just the line number in functions.php file (child theme) where I placed your code.

Viewing 15 posts - 1 through 15 (of 20 total)
  • You must be logged in to reply to this topic.