0

I've got a little (big) problem.

Is there a solution to show only posts that have not the same custom field value (from a specific custom field key) ?

I have some post with a manual ID as custom field, and I don't want to show them into my loop, and keep a good pagination.

It's easy to skip this post (that have the same manual ID) but I cannot get a good pagination because I use continue; to skip them.

6
  • pre_get_posts with a meta_query. There are many examples on site, just use the site search. :-) Commented Sep 14, 2015 at 18:00
  • Okay, I have already look at this. But I don't know which custom field value I have to exclude before the loop so I cannot use this...
    – J doeyes
    Commented Sep 14, 2015 at 18:28
  • How are you creating the custom field? Commented Sep 14, 2015 at 18:42
  • When I make a post I add a Custom field (key = Custom_ID, value = manual). This custom field can be the same for different post. I use a custom post like this to recognize same post in different state (<=> same content but with a little difference, for example not the same categories).
    – J doeyes
    Commented Sep 14, 2015 at 18:46
  • Now, I don't want to show more then one post that contain the same Custom_ID. I already done this like this : pastebin.com/feQtFYwC. But in archive page I have a pagination problem ... ((( Sorry for double post, I cannot edit after 5 mins, I don't know yet all rules here, sorry about that..)))
    – J doeyes
    Commented Sep 14, 2015 at 18:53

1 Answer 1

1

As I stated in comments, you need pre_get_posts and a meta_query to exclude posts from a certain custom field with a certain value from the main query. It is really in your best interest to read up on the before mentioned. Just remember, all parameters in WP_Query works on pre_get_posts as WP_Query uses pre_get_posts to alter the query parameters, and the main query uses WP_Query

In short, you can try the following: (REQUIRES PHP 5.4+ and the code is untested)

add_action( 'pre_get_posts', function ( $q )
{
    if (     !is_admin() // Targets the front end only, we don't need this for is_home()
          && $q->is_home() // Change this to the appropriate page, here we only target the home page
          && $q->is_main_query() // Target the main query only
    ) {
        $meta_query = [
            [
                'key' => 'Custom_ID',
                'compare' => 'NOT IN' // Do not get posts from this spoecific key/value pair
            ]
        ];
        $q->set( 'meta_query', $meta_query );
    }
});
6
  • Thank you firstly, you are helpfull. Sorry for incomprehension.. When I said "manual" that's mean that I add mannualy a key (-> like '32555', or '32999' for example). But I'll test this, your code can alter the wp_query loop after each iteration, is it this ?
    – J doeyes
    Commented Sep 14, 2015 at 19:01
  • I have updated the code to include all values from the custom field. This will only work for the main query, not for custom queries. On what page do you need to use this exactly. You should not run custom queries unnecessarily. Commented Sep 14, 2015 at 19:09
  • In my home page I use the pastebin code above, it's working good, no need more.Now, let's check that we are in the same wave length (french expression, I hope it's the same in english haha) : s3.amazonaws.com/aww-imagedata/… . Like you see, skip post is not a problem, but keep pagination is really hard.. I have this problem into archive / category page, but I'll use a custom search page, and I'll need a custom query here too... I hope you understand me, because I'm not the best english speaker man in the world like you can see.. :/
    – J doeyes
    Commented Sep 14, 2015 at 19:18
  • You should not be using custom queries as your main loop, this is always a problem in many ways, switch back to the default loop. As for the other pages, just extend the action's condition to include those pages. I'm off to bed now as it is quite late, but take your time and work though this post, it should clear up a few things for you ;-). Good luck Commented Sep 14, 2015 at 19:25
  • Thank you for advise Pieter, good night, maybe see you soon !
    – J doeyes
    Commented Sep 14, 2015 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.