Home › Forums › Pro Support › Can I exclude with a custom meta field from a list?
Tagged: "Meta key", "Meta value", "More settings"
- This topic has 18 replies, 3 voices, and was last updated 2 years ago by
elvin.
-
AuthorPosts
-
February 5, 2020 at 12:26 pm #13205
Xavier
ParticipantHello 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!
February 6, 2020 at 6:12 pm #13239Tom
KeymasterHi 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 🙂
February 7, 2020 at 5:53 am #13254Xavier
ParticipantThanks 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
February 11, 2020 at 5:12 pm #13344Tom
KeymasterHmm, 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.September 1, 2021 at 7:07 am #33380Xavier
ParticipantHello, 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
September 1, 2021 at 8:24 pm #33399elvin
ModeratorHi Xavier,
Can you try changing
"compare" => "!==",
to “compare” => “!=”?September 2, 2021 at 2:28 am #33405Xavier
ParticipantHi 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
September 2, 2021 at 7:47 pm #33440elvin
ModeratorBut 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",
September 3, 2021 at 1:12 am #33449Xavier
ParticipantDo 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
September 5, 2021 at 9:24 pm #33497elvin
ModeratorI 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 withwp_show_posts_shortcode_args
as Tom specified on one of his replies here.September 6, 2021 at 1:10 am #33504Xavier
ParticipantHi 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
September 6, 2021 at 1:13 am #33506elvin
ModeratorDid 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.
September 6, 2021 at 3:15 am #33508Xavier
ParticipantNo, 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.
September 6, 2021 at 11:17 pm #33536elvin
ModeratorElvin, 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.
September 7, 2021 at 2:14 am #33550Xavier
ParticipantI’ll give it a try
Thanks very much -
AuthorPosts
- You must be logged in to reply to this topic.