0

I have 2 area of post to show in each page.

first is on the widget (it's on the top of each page).

second is on the posts area (it show below the widget).

It's separated queries. article on the widget is another query and article on the posts area is main query.

So,I don't want it to have same article in both area (not duplicate) and articles on the widget are random to show.

I would like to use the article id which i got from the widget to modify the main query.

For example: i have article id number 1 , 4 and 10 and i would like to use it to modify the main query to make it not load that article id to show.

I got $array_widgetId from another file which has a loop to show the article in the widget(another query) and i set it to global variable and define it in this file which is used for show the article in the posts area(main query here) and also modify the main query to exclude that article id from loading.

while(have_posts()){

    the_post();
    //article to show in the posts area.
}

How can i reach my point to make it doesn't have duplicate article in both area without using pre_get_posts?

because i have tried to pass the array of widget id to use in function.php but it's not possible.

More information: I have many file to make this thing work(may be that's why it make me so confused to maintain).

I have one file to loop article to generate column.

"_posts.php" in warp framework layout

 $column_order = $this['config']->get('multicolumns_order', 1); 
    $colcount = is_front_page() ? $this['config']->get('multicolumns', 1) : 1;
    $widget_id (this array contains id of article which show on the widget kit)
            while (have_posts()) {
              the_post();
                 if (isset($widget_id) && !in_array($post->ID, $widget_id)) {
               if ($column_order == 0) {
                    // order down
                    if ($row >= $rows) {
                        $column++;
                        $row = 0;
                        $rows = ceil(($count - $i) / ($colcount - $column));
                    }
                    $row++;
                } else {
                    // order across
                    $column = $i % $colcount;
                }

                if (!isset($columns[$column])) {
                    $columns[$column] = '';
                }
                $columns[$column] .= $this->render('_post', array('is_column_item'  => ($colcount > 1)));


                $i++;
                if ($i == $default_posts_per_page) {
                    break 1; // break out 'while' loop level
                }
             }
          }
         // render columns
         if ($count = count($columns)) {
            echo '<div class="uk-grid" data-uk-grid-match data-uk-grid-margin>';
            for ($i = 0; $i < $count; $i++) {
                echo '<div class="uk-width-medium-1-' . $count . '">' . 
         $columns[$i] . '</div>';
            }
            echo '</div>';
        }else{
           // generate standard column
        }

Above code will generate column following setting in backend. After this, it will call "_post.php" and that "_post.php" will contain the HTML tag of my custom layout.

  • This makes no sense. The main query is related to requested permalink. I think you need to modify widget query to not duplicate main query, not the other way around. – Milo Aug 15 '17 at 13:06
1

Rather than trying to set a global variable, just use the main query for both the widget and the other content. Simply adjust your Loop.

If you share more of your code perhaps we can help specify where this would go, but the general idea is to move the start of the Loop above wherever you need to display the widget. Use a counter to keep track of whether you're looping through the first item (if so, show it in a widget) or not (show it in the main content area).

<?php get_header();
//
// your code - perhaps an outer container div, etc.
//
$counter=0; // set a counter
if(have_posts()): // start of normal Loop
while(have_posts()): the_post();
$counter++; // increment counter at each iteration
if($counter==1) { // only for first Post
    ?><div class="sidebar">
        <div class="widget">
            <?php the_title(); // add link, etc. as desired ?>
        </div>
    </div><?php
} else { // now show the rest of the Posts outside of the widget
    ?>
        <div class="maincontent">
            <?php the_title(); the_content(); // format as desired ?>
        </div>
    <?php
}
endwhile;
endif; // end Loop
//
// your code - perhaps an outer container div, etc.
//
get_footer(); ?>
0

In your loop, you can just exclude those posts by checking if the post ID is in the array of widget post IDs:

while(have_posts()){
  if(!in_array($post->ID, $array_widgetId)) {
    the_post();
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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