-1

I'm creating a Wordpress blog application using timber and twig. The problem is that i want to select all post except for one This is what I have now:

$context = Timber::get_context();
$context['post'] = new Timber\Post(); //the one we don't want

$args = array(
    'posts_per_page' => 2,
    'post_tag' => $context['post']->post_tag
);

$context['relevant'] = Timber::get_posts($args);//the ones we want

Timber::render(array('single-' . $post->post_type . '.twig', 'single.twig'), $context);

context['post']: contains one single post.

context['relevant'] contains all the posts with te same tag as context['post']

what i exactly want is: if the context['post'] element, is included in the context['relevant'], I want it removed and put in an other post in it instead.

Is there any way to do this?

Any help is much appreciated and thanks in advance.

SOVED

actually really easy, the only thing I had to do was to get het index of the array and then unset is.

$a = array_search($context['post'], $context['relevant']);

unset($context['relevant'][$a]);
timo stevens
  • 372
  • 1
  • 3
  • 15
  • Unclear what you're asking/trying to do... What does `$context['relevant']` and `$context['post']` contains? – DarkBee Feb 12 '19 at 10:12
  • is this more clear? – timo stevens Feb 12 '19 at 10:22
  • If you can precisely describe what your code should do, what keeps you from writing it? – Nico Haase Feb 12 '19 at 10:24
  • please verify once your Wordpress version with PHP version compatibility. – Prince Kumar Dwivedi Feb 12 '19 at 10:24
  • beacause I get errors @NicoHaase – timo stevens Feb 12 '19 at 10:26
  • my Wordpress and PHP are both compatible @PrinceKumarDwivedi – timo stevens Feb 12 '19 at 10:27
  • 1
    Your description is still rather unclear. If `context['post']` contains a full WP post object, it makes no sense to try and use that as an array index (referring to `$context['relevant'][$context['post']]`). _“context['relevant'] contains all the posts”_ - in what exact structure? Like using the post id as index, or - something else? – 04FS Feb 12 '19 at 10:27
  • 1
    So you want to remove a specific post from your array with posts? Then you would need to search the array first for the right [index](https://stackoverflow.com/questions/2959222/get-the-index-value-of-an-array-in-php) to unset – DarkBee Feb 12 '19 at 10:41
  • makes sense, i've edited the question so it is more clearly @04FS – timo stevens Feb 12 '19 at 10:41
  • If you "get errors", can you share them? Any error message would help to understand the problem – Nico Haase Feb 12 '19 at 10:46

1 Answers1

0

use isset function to not throwing errors

if(isset($context['relevant'][$context['post']])){
    unset($context['relevant'][$context['post']]);
}