[Resolved] Can I exclude with a custom meta field from a list?

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Can I exclude with a custom meta field from a list?

Viewing 15 posts - 1 through 15 (of 19 total)
  • Author
    Posts
  • #13205
    Xavier
    Participant

    Hello there!

    Since a specific taxonomy can’t be excluded from a list (https://wpshowposts.com/support/topic/exclude-category/) I’m trying to exclude the posts I need to ignore using meta Key and Meta Value in “More settings”.

    But is not working. :´(

    My web site has a number of posts now and it would be cumbersome to add a taxonomy or a custom meta field to all. So I add a field named “exclude” with the value “true” to the posts I want to discard from this specific list.

    My Question: can it be done somehow?

    Thanks!

    #13239
    Tom
    Keymaster

    Hi there,

    You should be able to choose exactly which categories you want to include posts from. This way, you can simply leave the ones you want to exclude un-checked.

    Let me know 🙂

    #13254
    Xavier
    Participant

    Thanks for your quick answer, Tom,

    Well, not ideal since we’re using several taxonomies and in each one, the post We want to exclude from the list share some other term(s) with others that must show up. Of course, we can create another taxonomy and apply to each and one of the posts (some 800) the appropriate term.

    If we can’t exclude employing Custom post Field we’ll try this approach.

    Thanks, Tom

    #13344
    Tom
    Keymaster

    Hmm, it may be possible otherwise.

    Make this change in your wp-show-posts.php file: https://github.com/tomusborne/wp-show-posts/commit/286caf1164db8b6b6f38b85d3a011b519a27f4de

    Then do this:

    add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
        if ( 123 === $settings['list_id'] ) {
            $args['meta_query'] => array(
                array(
                    'key' => 'your_custom_field',
                    'value' => 'your value to check',
                    'compare' => '!==',
                )
            );
        }
    
        return $args;
    } );

    You just need to change 123 to your list ID, and adjust the key and value.

    #33380
    Xavier
    Participant

    Hello, Tom (again…),

    It’s been a while since your last message, it’s my fault for not answering you, sorry. I don´t remember very well but I think I found a workaround for the first issue.
    BUT, here I am with a new question involving the same filter (wpsp_query_args) you introduced in version 1.2.0.
    I’m trying to filter a taxonomy in a list (id 50023) according to the meta key ‘idioma’ and the value ‘english’ with compare ‘!==’.. but it doesn’t work.
    Can you help me, please ? That’s the code I’ve placed in functions.php:

    
    add_filter(
      "wpsp_query_args",
      function ($args, $settings) {
        if (50023 === (int) $settings["list_id"]) {
          $args["meta_query"] = [
            [
              "key" => "idioma",
              "value" => "catala",
              "compare" => "!==",
            ],
          ];
        }
    
        return $args;
      },
      10,
      2
    );

    Thanks

    #33399
    elvin
    Moderator

    Hi Xavier,

    Can you try changing "compare" => "!==", to “compare” => “!=”?

    #33405
    Xavier
    Participant

    Hi Elvin,

    I’m sorry to say that it’s not working…

    On the other hand, I’ve tried another more classical approach which is using the action “pre_ger_post()”:

    
    add_action("pre_get_posts", "etdla_pre_world_arts");
    function etdla_pre_world_arts($query)
    {
      if (!is_page("world-arts")):
        return;
      endif;
    
      if (!is_admin() && $query->query_vars["post_type"] === "post"):
        $meta_query = [
          [
            "key" => "idioma",
            "value" => "catala",
            "compare" => "!=",
          ],
        ];
        $query->set("meta_query", $meta_query);
        $query->set("meta_key", "idioma");
      endif;
    }
    

    But that’s not ideal since I’m not taking advantage of the plugins features.

    Thanks

    #33440
    elvin
    Moderator

    But that’s not ideal since I’m not taking advantage of the plugins features.

    Yeah that’s true. And this applies to all the post list in that specified page. (incase you have multiple post lists)

    What happens when you try this?

    "key" => "idioma",
            "value" => "catala",
    			"compare" => "NOT EXISTS",
    #33449
    Xavier
    Participant

    Do you mean this?:

    function etdla_pre_world_arts($query)
    {
      if (!is_page("world-arts")):
        return;
      endif;
    
      if (!is_admin() && $query->query_vars["post_type"] === "post"):
        $meta_query = [
          [
            "key" => "idioma",
            "value" => "catala",
            "compare" => "NOT EXISTS",
          ],
        ];
        $query->set("meta_query", $meta_query);
        $query->set("meta_key", "idioma");
      endif;
    }

    Then outputs no posts

    #33497
    elvin
    Moderator

    I mean like this.

    add_filter( "wpsp_query_args", function ($args, $settings) {
        if (50023 === (int) $settings["list_id"]) {
            $args["meta_query"] = array(
                array(
                    "key" => "idioma",
                    "value" => "catala",
                    "compare" => "NOT EXISTS",),
                    );
        }
        return $args;
    },10,2);

    But tbh I think != should work.

    Example:

    add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
        if ( 50023 === $settings['list_id'] ) {
            $args['meta_query'] => array(
                array(
                    'key' => 'idioma',
                    'value' => 'catala',
                    'compare' => '!==',
                )
            );
        }
    
        return $args;
    } );

    I basically just replaced wpsp_query_args from the code you provided with wp_show_posts_shortcode_args as Tom specified on one of his replies here.

    #33504
    Xavier
    Participant

    Hi there again,
    I’m sorry Elvin, this code gives an error about the number of arguments passed:

     Fatal error: Uncaught Error: Too few arguments to function {closure}(), 1 passed in (... a path here...) /wp-includes/class-wp-hook.php on line 303 and exactly 2 expected
    in (... a path here...)/wp-content/themes/tempsart-gp-dispatch/functions.php on line 2134
    
    Call stack:
    
    {closure}()
    wp-includes/class-wp-hook.php:303
    WP_Hook::apply_filters()
    wp-includes/plugin.php:189
    apply_filters()
    wp-content/plugins/wp-show-posts/wp-show-posts.php:383
    wpsp_display()
    wp-content/plugins/wp-show-posts/wp-show-posts.php:569
    wpsp_shortcode_function()
    wp-includes/shortcodes.php:356
    do_shortcode_tag()
    preg_replace_callback()
    wp-includes/shortcodes.php:228
    do_shortcode()
    wp-includes/class-wp-hook.php:303
    WP_Hook::apply_filters()
    wp-includes/plugin.php:189
    apply_filters()
    wp-includes/post-template.php:253
    the_content()
    wp-content/themes/generatepress/content-page.php:73
    require()
    wp-includes/template.php:772
    load_template()
    wp-includes/template.php:716
    locate_template()
    wp-includes/general-template.php:204
    get_template_part()
    wp-content/themes/generatepress/inc/theme-functions.php:591
    generate_do_template_part()
    wp-content/themes/generatepress/page.php:34
    include()
    wp-includes/template-loader.php:106
    require_once()
    wp-blog-header.php:19
    require()
    /home/imvzwgev/public_html/index.php:17

    Thanks

    #33506
    elvin
    Moderator

    Did you apply the changes Tom suggested on this reply? https://wpshowposts.com/support/topic/can-i-exclude-with-a-custom-meta-field-from-a-list/#post-13344

    It’s required so the filter can accept variables.

    #33508
    Xavier
    Participant

    No, I didn’t, but now I’ve done and got the same results as shown in this screenshot:
    https://snipboard.io/kAqPjZ.jpg
    The change has been made in
    /wp-show-posts/wp-show-posts.php
    as you can see here:
    https://snipboard.io/A1k8TI.jpg
    but to no avail.

    Elvin, in fact the page is working with “pre-get-posts”, and I’m taking too much of your time. I’m really sorry. You can mark this ticket as closed if you want.

    #33536
    elvin
    Moderator

    Elvin, in fact the page is working with “pre-get-posts”, and I’m taking too much of your time. I’m really sorry. You can mark this ticket as closed if you want.

    No problem. But it’s a good thing to solve because pre_get_posts can present issues if you decide to put another post list on a page as you can’t target specific loops within a page.

    But I guess if you don’t plan on adding another loop of different query on a single page, that’d be fine.

    I find this strange.

    I was able to make this one work on my test site.

    add_filter( "wp_show_posts_shortcode_args", function ($args, $settings) {
        if (4581 === (int) $settings["list_id"]) {
            $args['meta_query'] = array(
    			'relation' => 'OR',
                array( 
    				'key' => 'idioma',
    				'compare' => 'NOT EXISTS'
    			),
    			array( 
    				'key' => 'idioma',
    				'value' => 'catala',
    				'compare' => '!='
    			),
    		);
        }
        return $args;
    },10,2);

    What this does is, if the custom field “idioma” is empty or the value is “catala”, it excludes it on the loop.

    Replace 4581 with your WPSP list ID.

    #33550
    Xavier
    Participant

    I’ll give it a try
    Thanks very much

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