Reduplicate by MEOW_FieldSetCollection value on collection

This topic is: not resolved

Tagged: 

Viewing 5 posts - 1 through 5 (of 5 total)
Author Posts
November 5, 2014 at 3:09 pm #3555
sitesurety
Post count: 13

I have a collection of MEOW_FieldSetCollection objects and want to filter the collection on a value of a custom field.

I can’t seem to pass a string to the dedupe() method to filter the collection of objects. How do I filter the collection based on duplicate attribute values?

November 5, 2014 at 4:55 pm #3557
traversal
Post count: 207

So just to clarify, I presume you’re trying to dedupe by passing in a full-field string. As in “set_name.field_name”?

That actually won’t work currently unfortunately, as the dedupe method is currently setup to allow expressions that resolve to a property through a slightly different mechanism. A “set_name.field_name” is a bit different, as it uses a runtime evaluated expression to get the value.

It actually ought to work with anything, and I’ve noted it down to be fixed. Until then though, I believe something like this should work:

https://gist.github.com/traversal/6a13e7786d168d7588db

Basically you can extend the post class, and then give it a named method which can then be used to dedupe the posts.

Let me know if that helps.

November 5, 2014 at 8:21 pm #3558
sitesurety
Post count: 13

Seems to have. I did have to make some slight changes, mainly around casting to a string so the function accepted the dedupe rule:

foreach ($wf->type(“person”)->posts->dedupe(“person_type”) as $key) {
echo $key->person->type; // Outputs a unique person type list
}

In functions, I had to cast to a string:
class MY_Post extends MEOW_Post {

function person_type() {
return ((string) $this->person->type);
}

}

November 5, 2014 at 8:23 pm #3559
traversal
Post count: 207

Ahh, yep, you’re right. You can also use $this->person->type->val();

Sorry about the workaround, but I’m glad you got it going.

November 5, 2014 at 8:32 pm #3560
sitesurety
Post count: 13

Thanks for the help! Very much appreciated. I want to keep my code as slick and clean as possible.. I like how the MP plugin is very Java collection-ishs.

Two other things I’ve been doing which I’m not entirely happy about:

Creating a multi-dimensional array for all the filtered options by data type, which I then output to a form using a double loop:

$filters = array(
“type” => array(),
“hair” => array(),
“blah” => array()
);

// Loop code with de-dup here as $key
if ($key != “”) {
$filters[“type”][] = $key->person->type->val();
}

Then create a form with all the unique filter options:

<!– Filter form –>
<form action=”” method=”get”>
<?php foreach ($filters as $key => $array) { ?>
<label><?php echo ucwords($key) ?>:<label>
<select name=”<?php echo ucwords($key) ?>”>
<?php foreach ($array as $id => $value) { ?>
<option value=”<?php echo $value ?>”><?php echo $value ?></option>
<?php } ?>
</select>

<?php } ?>
</form>

Then I’ve got some jQuery over the top which responds when an option is selected and posts the updated id => keys via Ajax back to the form, which updates the results table:
$(function() {
$(‘select’).bind(‘change’, function(e) {
$(this).closest(‘form’)
.trigger(‘submit’);

$.ajax({
type: “POST”,
dataType: “json”,
data: JSON.stringify(getValues()),
//dataType: “html”,
cache: false,
success: function(data){
$(“results”).html(data);
}
});
return false;

});

// Return select name and value
function getValues() {
var options = $(‘select’);
var values = $.map(options ,function(option) {
return new Array(new Array(option.name, option.value));
});

return values;
};

});

Ideally, I was thinking of moving the data filtering into the controller (functions.php) file, with different case rules…so I just have to output the view in the template.
Is there any other way master press could optimise this/shorten the code?

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

Latest From the Blog

MasterPress 1.4.1 is now available

27th May 2024

This update addresses backwards compatibility issues that were introduced in the previous update. There were some edge cases where PHP 7 had issues and it didn’t work at all on PHP 5.6. We have now changed it so that the PHP 8 compatibility changes will now only affect PHP 8 and will be more thorough… 

Plugin Requirements

MasterPress requires a minimum of WordPress version 4.9, MySQL 5.6, and PHP version 5.6.20.

We also recommend that PHP is configured to use a memory limit of 64MB per request (128MB may be required for sites with higher complexity).

This plug-in is not compatible with the WordPress.com hosted service.

Three AM