Using by reference of WP_Query in posts_search filter hook?

Problem:

User is working on a plugin. He was trying to change the WP_Query fired by the requested page.The query itself is based on several _REQUEST parameters he can use to detect and manipulate because they are unique for his plugin.

At first he thought to use the “posts_search” filter hook in his plugin. This hook seemed good as it is passing the WP_Query object by reference. He managed to determine and change the WP_Query object to reflect the new query parameters for his purpose. Unfortunately, this approach didn’t work because the changed WP_Query object was not used for the remainder of the get_posts() method. Instead, it is using $q, which is the shorthand for query_vars: “$q = &$this->query_vars“. Does anyone know how to use the by reference value of the WP_Query object properly for this purpose of manipulating the query settings?
The same applies to other filter hooks in the get_posts() method: “posts_where“, “posts_where_request“, “posts_request” and “posts_results“.

Solution 1:
Have you tried using the pre_get_posts hook?
http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
Do use it with caution as you can end up modify all queries at once, but usually it’s a pretty good way to override things.

Solution 2:
Use the pre_get_posts hook. Then use the ->set() method to set query variables.

It passes in the WP_Query object which has a set method.
function my_callback($query) {
$query->set('foo', 'bar' );
}

Conclusion: 2nd solution is preferable for this problem. So use that one.

WordPress TinyMCE : Wrap several elements in DIV

Problem: User wants to know if it is possible to wrap multiple items in a div by using TinyMCE? He wants to avoid using a shortcode.
Here is a setup:

$style_formats = array(
array(
‘title’ => ‘Title with list’,
‘block’ => ‘div’,
‘classes’ => ‘my-class’,
‘wrapper’ => true,
),
);

The result:

Title

  • Some text...
  • Some text...
  • Some text...

But he wants to has something like this:

Title

  • Some text...
  • Some text...
  • Some text...

Solution 1:
I think you have to use a different method to do this. Based on the documentation here:
https://www.tinymce.com/docs/configure/content-formatting/#formats

Take a look here:
https://codex.wordpress.org/TinyMCE_Custom_Buttons

And Here:
http://code.tutsplus.com/tutorials/guide-to-creating-your-own-wordpress-editor-buttons–wp-30182
I haven’t actually done this – I just spent some time researching it a while ago for a client.

Solution 2:
After slightly modified the code User has what he wanted.
He also added a button to remove the div.
Here is the method:
http://community.tinymce.com/forum/viewtopic.php?id=19973

Conclusion:
Above problem is solved by user himself so 2nd solution is correct.but if you have any other idea to solve this problem you can do that. Be creative.

Retrieve category_id’s in the loop

Problem:
User is looking for the right function to retrieve all category_id’s of a specific post within the loop.
He wants to store the id’s in a variable or an array.

Following is the solution of above stated problem.
Solution: We have finally found the function to solve the problem.

foreach((get_the_category()) as $cat)
{
echo $cat->cat_ID;
}

Conclusion:
Its solution is very simple. You have to just use above code in your website and enjoy the results.

Generate page title from the content of H1

Problem: User wants to know that Is there any way of creating the title tag of a page from the content of an H1 tag on his wordpress page? He has got some dynamically generated content used in through an external XML stylesheet. At the moment, page isn’t physically generated using the wordpress admin system and it is just using a template. The page title is just showing the same related to the content.

However, he was just checking whether it would be possible in php to grab the content of an H1 on a page and used it as a page title?Any tips or advice would be greatly appreciated.

Solution 1: Is the H1’s or heading content coming from the XML too? If so user could use a quick regEx on that and echo the value out in the title tags.

Solution 2: It seems like user wants to pull feed content into wordpress as a wp-post.
http://wordpress.org/extend/plugins/feedwordpress/ used to work for this.

Conclusion:
Both of the solutions can solve your problem. Its up to you which one you’ll use.

[Plugin: Visual Form Builder] Digit validation

Problem:

Our problem is related to visual form builder plugin. User wants to know that Is there any way to organise a specified number or characters used to validate the number field when specifying to validate digits?
He wants to use just only “Number” field to get ZIP codes validation for digits. But the “Number” field have no limit for number of characters to check against them. It will validate everything as as long as it is a digit or number.
Would that be a rule he writes in the my-js.js file? If so how?
Every problem has a solution. So for solution look forward.

Solution :

jQuery(document).ready(function($) {
$.datepicker.setDefaults({
dateFormat: 'mm-dd-yy',
numberOfMonths: 1,
changeMonth: true,
changeYear: true,
yearRange: "c-90:c+1"
});

$("#newsletter-sign-up").validate({
rules: {
'vfb-zip-code-95': {
required: true,
minlength: 5,
maxlength: 5
}
}
});
});

Conclusion:
In wordpress, many problems arises but they all have solutions. Every programmer has its own logic and way of understand things. But we are going to give you precise and accurate solutions for your problem.