This use of posts_where() function is not clear - wordpress

I have this very short function that I do not understand at all, and the comment is laughable:
// This function makes the meta query work
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'bonuses_%", "meta_key LIKE 'bonuses_%", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
The meta query in question is a standard WP query with array_push($args['meta_query'],$bonusesArgs); pushing $bonusesArgs (the meta query) into it.
Without the my_posts_where function, the meta query doesn't work. And I have no idea why (and neither did the developer apparently...!)
I am hoping that someone can explain the my_posts_where function - what is it doing, I've looked all over and can't make sense of it.

Well.. all it seems to be doing is rebuilding the meta query in a different, but using LIKE instead of a direct =
Like performs a search for the value in the string, instead of a direct equality.
So it would seem that this function is completely superfluous, if the original meta query just used the compare = 'LIKE' value.
But without seeing the entire code, it'd be really tough to tell exactly what repercussions this might have. But no matter what, there are better ways to interact with a query object than running some weird str_replace on its data.

Related

Wordpress get custom fields value

So I am trying to get custom post values in WP. The thing is I can have multiple custom fields value. So I don't want to hard code in the code. What I am thinking of doing is providing the a prefix in the key value something like
abc_email
abc_website
So I want to use this function
get_post_meta
or some other to get all the key,value pair that starts with abc. That way I can just add values on the back end and don't have to update my code. One way would be to get all the meta data using above function and then loop over it and filter. But is there any other way where I can send in the pattern I am looking for?
Thanks
You could directly query the database for a posts entries containing the prefix (relevant table etc here https://wordpress.stackexchange.com/questions/104434/where-are-custom-field-values-stored-in-the-database )
but this is more complex, still involves a loop and doesn't provide you with anything you could do using WP and PHP. If you want to cater for unknowns you will have to loop or interogate an index etc.
I may be missing something in your question but this seems straight forward:
You are trying to avoid this scenario:
process_field($abc_known1);
process_field($abc_known2);
// continually edit code for new entries e.g.:
process_field($abc_NEW1);
Something like this will handle later additions without modification.
// get array of ALL cust fields for post
$all_cust_fields = get_post_meta( get_queried_object_id());
foreach ($all_cust_fields as $key => $value) {
// some function for determing if $value starts with "abc_"
// if so either:
// add to an "abc" array for later use/processing
// or take action in this loop e.g.
process_field($value);
}
I am not sure whether you are talking about keys or values prefixed "abc_" but the same principle applies.
you should write code like this,
$email_meta = get_post_meta( 'your_post_id', 'abc_email', true );
$website_meta = get_post_meta( 'your_post_id', 'abc_website', true );
Or
you can follow these links,
example
example
Hope this will help you.

Symfony querybuilder compare string when not empty

I am new to CreateQueryBuilder and I have a quite complex query with nested and->or->and condition. It almost works (thanks Patrik Karisch) but I am blocked by a simply and "stupid" question. I want to compare a string column only when the value from column or parameter is not empty. (Note: I cannot use if and add, I have to use logic inside orX).
$expr->neq('s.studentEmail', '') always returns true when the vale is '', but I am expecting false
$queryBuilder = $repo->createQueryBuilder('s');
$expr = $queryBuilder->expr();
->andWhere($expr->orX(
$expr->andX($expr->neq('s.studentEmail', ''), $expr->isNotNull('s.studentEmail'), $expr->like('lower(s.studentEmail)', 'lower(:email)'),
$expr->......
I think you might be over complicating things. You can test your parameter outside of the query and simply skip adding the condition at all;
$qb = $studentRepo->createQueryBuilder('student');
if ($email) {
$qb->andWhere('student.email LIKE %:email%');
$qb->setParameter('email',$email);
}
I also don't see the need for the lower functions but put them in if you like.
I started using expressions because that is the way the documentation shows but not really got comfortable with them. Very verbose. Now I just write the DQL out as shown above. Seems to work the same and is a bit easier to read.
Finally, you should define your email column to either be NOT NULL in which case all empty emails are blank strings. Or allow it to be NULL and ensure that empty strings are always entered as NULL. Mixing the two together can over complicate things.

How do I test for an invalid WordPress WP_Query query?

I have a page template which displays a list of post summaries, depending on a user-defined custom query, which is supplied using the post_content, for example:
category_name=blog
This is then passed to WP_Query() which will return however many 'blog' posts there are.
However, if I pass a completely invalid query (or simply make a mistake) such as:
the rain in spain falls mainly on the plain
WP_Query->get_posts() will return ALL posts in the database, rather than letting me know that the query is meaningless / erroneous.
Is there a built-in method to test for invalid queries?
Currently, I'm doing a parse_str() to convert the query to an array, then an array_intersect_keys() against a list of valid query parameters I've compiled from the WP_Query page.
It works, but feels pretty hacky - am I missing something?
Thanks,
Dave
I don't believe there is a failed query catch built in to WP like you are asking for (I wish there were, too).
However, there may be a cleaner hack. wp_count_posts will return a count of all posts in the database. And wp_query will return a count of its results in $post_count. One thing you could do is a check on your query - if the result of wp_count_posts is equal to $post_count then you know something is wrong.
It's not as clean as a 'no results' response, but perhaps more straight forward than your current solution.
http://codex.wordpress.org/Function_Reference/wp_count_posts
http://codex.wordpress.org/Class_Reference/WP_Query (search the page for $post_count)

What do i put in get_permalink(get_option(' here???')

I'm trying to work out what the option value is in get_permalink(get_option('jr_dashboard_page_id') I'm trying to follow this convention but cant work where it comes from - dashboard is the post-name in the DB what the rest of it means or how its works I cant figure out...
Any help would be great..
The Options API stores key-value pairs. For example, add_option( 'key', 'value' ); could be used. In that case, get_option( 'key' ); would return value.
So at some point a plugin or the themes that you use defined jr_dashboard_page_id to be something. Perhaps via a meta box or something like that. There is not enough information in this question to say exactly what "key" you need. It's like saying, I have an associative array $array['key'] what key should I use? We simply can't know because we don't know how array looks and we don't know what kind of value you expect.

Drupal: change view argument

I searched far and wide to find a working solution to this but couldn't find it.
What I want to do is change the argument that is passed to the view because I want for pathauto cleaned taxonomy terms to work as an argument. I have a code that transforms cleaned term back to a original one, but cannot make the view use it.
I saw some people changing it in hook_preprocess_views_view(&$vars) but in my case (Views 2.11) has a argument in $vars instanced so many times that it's not the way.
Can anyone please help me change this argument?
There may be a better way but you could use views_embed_view() and set the arguments yourself
I have two ideas, either to add some custom php code to the view's argument's phpcode section that does something like this
$args[0] = 1;
return $args;
or try to use the function
hook_views_pre_view(&$view, &$display_id, &$args) {
// modify $args value here
}
didn't test them so don't know which will work.
I think hook_views_pre_view might help you do just that.

Resources