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.

What does “object_id” means?

Problem: User wants to know about object id. He read database description- http://codex.wordpress.org/Database_Description#Table:_wp_terms, and get the table description:

Table: wp_term_relationships
Field Type Null Key Default Extra
object_id bigint(20) PRI Pt1 0
term_taxonomy_id bigint(20) PRI Pt2, IND 0

He know term_taxonomy_id is FK->wp_term_taxonomy.term_taxonomy_id, but how about object_id?
He has a further question:where are “object_id” from? He checked the table “wp_posts” and “wp_links” and found no fields relative to “object_id”.

Solution 1: A quote from: http://boren.nu/
“object_id” is the ID of a post or link. “term_taxonomy_id” is an ID from the term_taxonomy table designating a particular term+taxonomy pair.

Solution 2: In wp_posts, there is an ID column. In wp_links, there’s a link_id column.Those are the connections to the object_id in the wp_term_relationships table. term_relationships basically acts as a connection between the objects (posts or pages) and the various terms attached to those objects (tags or categories or link_categories).

Solution 3: They are the table in question’s primary key. A word to the wise there can be duplicates!!!
My wp_term_relationships Shows the following records with an object_id of 10:
10 – 1
10 – 5
10 – 12
The first two records references a post in wp_posts that has an ID of 10. The third record references a link in wp_links that has a link_id of 10.

Conclusion:
I hope you understand object id from all above solutions. All the above solutions are very accurate. It is easy for anyone to understand object id from above stated solutions.

if logged in?

Problem: Problem is to design login page with your own requirements. How do you create a page so if you are logged in it says Welcome “user” on the pages instead of just the static or builtin “Login” page.

Solution: You’d need to know some PHP. Here’s how to access the username:
http://codex.wordpress.org/Function_Reference/get_userdata

Conclusion: Designing your website in a proper way is very important. So above codex will help you to design your login page in your desired layout

Only show posts if they’re less than 3 days old

Problem: User would like to add a “breaking news” just under the header on site’s custom front page. He wants to show only those posts that are less than 3 days old from a specific category.
His opinion is to put the entire mini-loop inside a php if statement then compare the post date to a number and only print the breaking news ticker if it is less than that number. If there aren’t any breaking news posts in three days, the ticker doesn’t show up any thing.
Users mini-loop working beautifully, posting the 6 most recent news posts on a static home page, using this code:

$my_query = new WP_Query('category_name=news&showposts=6');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
echo ''.the_title().'';
endwhile;

Solution:
// Start of the mini-loop, specifies which category to look in and how many posts to pull//
$my_query = new WP_Query('category_name=breaking-news&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
// Adjust 604800 to the number of days in seconds that you want to check against. Posts will only appear if they are newer than that time.//
if ( (current_time(timestamp) - get_the_time('U') - (get_settings('gmt_offset') * 3600) ) < 604800 )
{
echo "

Breaking News:";
the_title();
echo "

";
}
endwhile;

I hope this helps you out.

Conclusion: Above code will help you to solve your query. Without requirements or design, programming is the art of adding bugs to an empty text file. And finding bugs and solve them is also an art.

Fetching post content from all top-level terms in a custom taxonomy

Problem: User has been trying to figure this out for a while, searching the internet for solutions but not helpful. User has created a custom post type products, with a custom taxonomy of product_type.

What He is trying to do is create a query that will fetch all top level posts (in alphabetical order) in the product_type taxonomy and output the first n number of posts related to each taxonomy term. Example:

product_type A
– Post 1
– Post 2

product_type B
– Post 1
– Post 2

product_type C
– Post 1
– Post 2

He has been playing around with get_categories():
$cat_args = array(
'taxonomy' => 'product_type',
'type' => 'products',
'orderby' => 'name'
);

$categories = get_categories('taxonomy=product_type&type=products');
I have use this WP_query but unfortunately get_categories() doesn’t support the ‘depth’ parameter as available with wp_list_categories().

Solution :
Use the get_terms() function for this. Setting the ‘parent’ arg to 0 should cause it to return only top-level terms. Here’s what you will want to do:
$types = get_terms( 'product_type', array(
'parent' => 0
) );

// Any types?
if ( $types ) :
// Now start the loop.
foreach ( $types as $type ) :
// Do your WP_Query stuff here...
endforeach;
endif;

I hope that helps. You can read more about get_terms() here: http://codex.wordpress.org/Function_Reference/get_terms.

Conclusion:
Every problem has a solution. Above i have stated the solution of your problem. Programming can be fun, so can cryptography however they should not be combined. Keep practicing coding. Don’t bother about problems.

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.

New error notice for WP_Post::$ancestors in 3.5

Problem: This problem is with the newest version of WordPress. User has used this function without problems in the past, but in 3.5. He has received an error notice, which reads:
Notice: Indirect modification of overloaded property WP_Post::$ancestors has no effect in /mysite.com/public_html/wp-content/themes/mytheme/functions.php on line 09
Line 9 is:

return end($post->ancestors);
function get_top_parent_page_id() {

global $post;

// Check if page is a child page (any level)
if ($post->ancestors) {

// Grab the ID of top-level page from the tree
return end($post->ancestors);

} else {

// Page is the top level, so use it’s own id
return $post->ID;

}

}

The function he has found at:
http://www.webcitizenmag.com/2010/05/20/how-to-get-top-parent-page-id-in-wordpress/
Is this any bug in wordpress Or is there something that changed in the core code?
The function still works properly if he does not has set debug to true.

Solution 1:
Try replacing this code:
return end($post->ancestors);
With this:
return end( get_post_ancestors($post) );

Solution 2:
I think that this is due to the ancestors property is no-longer loaded by default.The thing is that when you call the ancestors property, PHP checks that it’s not a property that has been created for that WP_Post object. So PHP tries to call the __get() method of the class in the case of WP_Post exists. This is a magical method in PHP used to auto load properties for an object.
Can you try with this code:

function get_top_parent_page_id() {

global $post;

$ancestors = $post->ancestors;

// Check if page is a child page (any level)
if ($ancestors) {

// Grab the ID of top-level page from the tree
return end($ancestors);

} else {

// Page is the top level, so use it’s own id
return $post->ID;

}

}

Conclusion:
Both of the above solutions can solve that problem. You can use any one of them. So enjoy.

wp-login.php blank page

Problem:
In this problem user won’t be able to logged in to his WordPress website account. He has a problem regarding logging on his website that runs on WordPress. He upgraded WordPress to 3.3.1. Everything worked fine till this morning but now he couldn’t login anymore on WordPress, and he just got a blank page. The username and password are totally correct. He has deleted all the plugins from wp-content/plugins folder to see if there is a problem with them and made the upgrade again but no results found. He has installed the older version of WordPress, again, and still no result. Visually website is running very well. He cannot be able to access the Dashboard any more.

Solution :
Solution is very simple. Sometimes in hurry we made a little mistake and then unable to find that. User forget one step, to see if there is a problem with his custom theme. He has deleted the basic theme Twenty Ten from “wp-content/themes” and left his custom theme there. That custom theme has a bug, because every time he make some changes, add and modify articles and manage some comments it shows that blank page. Unable to refresh the page, or anything else.
So he renamed the folder “themes” to something else and then his Dashboard started to work. Here is a tip that renaming the folder “themes” will shows a blank page on your website and you can’t see anything else, so keep a basic theme on folder “themes” to avoid this mashup.

Conclusion:
As is stated above sometimes we made a little mistake and then unable to find that mistake. Same thing happens to the user but later on he found that mistake and solved his problem by his own.