Skip Navigation

[Resolved] How to remove posts displayed using post id filter

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a View that uses a post ID filter based on a URL parameter. I am able to use the URL parameter array to show multiple posts in the View results. I would like to be able to allow my Users to click a button next to any result to remove that result from the list, and show an updated list of results. For example, here is the URL format for the View:
@http://mysite.com/?post_ids%5B%5D=24&post_ids%5B%5D=23&post_ids%5B%5D=22&post_ids%5B%5D=21&s=

I would like to be able to click a button next to post ID 23 and end up at a URL like this:
@http://mysite.com/?post_ids%5B%5D=24&post_ids%5B%5D=22&post_ids%5B%5D=21&s=

Solution: I can't think of any easy way to do this without some JavaScript URL manipulation. Something like this:

jQuery('#wpv-supplier-comparison :submit').click(function(e){
  e.preventDefault();
  var url = window.location.href,
    val = jQuery(e.target).val(),
    firstVarString = '?post_ids%5B%5D=' + val,
    notFirstVarString = '&post_ids%5B%5D=' + val,
    firstVarPos = url.indexOf(firstVarString),
    notFirstVarPos = url.indexOf(notFirstVarString),
    pos = firstVarPos > -1 ? firstVarPos : notFirstVarPos,
    varString = firstVarPos > -1 ? firstVarString : notFirstVarString,
    urlArray = url.split(varString, 2),
    newUrl = urlArray[0] + urlArray[1];
 
  if(newUrl.indexOf('?') < 0) {
    newUrl = newUrl.substr(0,newUrl.indexOf('&')) + '?' + newUrl.substr(newUrl.indexOf('&')+1);
  }
  window.location.href = newUrl; 
});
This support ticket is created 6 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by davidR-12 6 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#902159
FireShot Capture 004 - Edit View ‹ 3D Net — Word_ - https___3dnet.foxrobinson.com_wp-admin_admin.php.jpg

I have a page here:

hidden link

The page is set to display posts using the post id filter (Image attached)

I have then set them to display with a button with a value of their post id:

<form id="wpv-supplier-comparison" action="[wpv-bloginfo show='url']/marketplace-enquiry-from-page/">
<input type="submit" name="post_ids[]" value="[wpv-post-id]">
</form>

clicking on this refreshes the page and removes all other posts.

What i want it to do is to refresh the page and remove the post that is clicked on rather than removing all other ones. How can I do this?

Regards,

David

#902213

Hi, I don't know of an easy way to do this without some JavaScript URL string manipulation. Something like this might be useful:

jQuery('#wpv-supplier-comparison :submit').click(function(e){
  e.preventDefault();
  var url = window.location.href,
    val = jQuery(e.target).val(),
    firstVarString = '?post_ids%5B%5D=' + val,
    notFirstVarString = '&post_ids%5B%5D=' + val,
    firstVarPos = url.indexOf(firstVarString),
    notFirstVarPos = url.indexOf(notFirstVarString),
    pos = firstVarPos > -1 ? firstVarPos : notFirstVarPos,
    varString = firstVarPos > -1 ? firstVarString : notFirstVarString,
    urlArray = url.split(varString, 2),
    newUrl = urlArray[0] + urlArray[1];

  if(newUrl.indexOf('?') < 0) {
    newUrl = newUrl.substr(0,newUrl.indexOf('&')) + '?' + newUrl.substr(newUrl.indexOf('&')+1);
  }
  window.location.href = newUrl; 
});
#902217

Hi Christian,

Thank you for that code which works perfectly! Thank you very much!

Cheers,

David