Wordpress query_posts to modify wp_query - wordpress

When I alter main $wp_query with query_posts function, all my conditional tags are false and all pages have [is_home] => 1.
So all custom templates I made by modifying main query to save some time are now home. Anyone knows a fix for this?

You should never use query_posts to create custom queries. query_posts breaks the main query, as you have seen. Whether or not to use a custom query for your specific needs, I don't know.
I have done a complete post about this subject on WPSE that you can go and check here. Also go and check out Theme Development in the codex

Related

Set wordpress query before template

I have a business goal forcing me to try to change the global wordpress query after the URL has been determined, but before the templates start outputting variables in the context of the original post. I need to be able to use a plugin to check some meta values on the original post, and then change the query to represent another post object to display different data without changing the url.
I've tried using setup_postdata() what seems like everywhere.
(tried including wp_reset_query();)
global $post;
$post = get_post(145, OBJECT );
setup_postdata($post);
However, the template is still outputting the original query.
I'm open to other solutions. Thanks in advance.
add_action('wp_loaded', function(){
query_posts(array('p'=>145,'post_type' =>'any'));
});
This worked out fine. It can be added just about anywhere. However, it messes up page templates, and displays pages as if they're single.php!!! If I can get around that, then I'll be in good shape. Help?
EDIT: Got it working. I have to check and use p for posts, and page_id for pages. So long as those are set, the templates will follow correctly. Otherwise it was trying to apply the standard post template to pages.

Page Template for every custom post type

I'm just new in wordpress, I just wanted to ask if it's a bad practice if I'll create page template for every custom post types?
I mean if I have CPT [custom post type] with different content, like my first CPT has images, my 2nd cpt has image and text, my last CPT, has slider, text and description.
Because I wanted to create page template for every type of CPT's I have. Is it a bad practice? Or are there efficient and effective ways to do such things?
Your answers are highly appreciated. Thanks!
It's better not to do that, not only because it's a server load, but also because you'll get crazy if you have an issue. Instead, use conditionals in the same page. You can read about it at WP Codex: Conditional Tags : Taxonomies and then pay attention to the is_tax and has_term tags.
This way, you can use is_tag or has_term depending on your approach, like this
if( has_term( 'jazz', 'genre' ) ) {
// do something
}
You can use taxonomy to deal with such problem. Use taxonomy to filter contents according to your requirements. Use register_taxonomy() to register a taxonomy for CPT.

Tags create/edit/delete hooks in wordpress

I need hooks if tags have removed, created and edited. I have not found them in the WordPress Codex / Function Reference.
For categories:
edit_category
create_category
delete_category
Has anyone encountered this problem?
To explicitly target tags, you'll need create_$taxonomy, edit_$taxonomy and delete_$taxonomy where $taxonomy is post_tag (ie create_post_tag, edit_post_tag and delete_post_tag). They're mentioned in passing in (eg) the wp_insert_term.
Tags are a taxonomy, so the generic actions create_term, edit_term, and delete_term would work as well, though they'll also fire for other taxonomies (such as categories).
You need to use dynamic hooks to taxonomies:
edit_post_tag
create_post_tag
delete_post_tag

WordPress functions.php: how to apply update_option()?

I'm trying to set the default Image Link URL for my WP users so that it doesn't include the url link as a default. I've done some research, and I know the function is in the wp-admin/options.php:
update_option('image_default_link_type','file');
Rather than mess with the core files, I'd like to put this into the functions.php, but never know the proper way to implement stuff like this! This is what I have so far in my functions.php:
<?php
update_option('image_default_link_type','none');
?>
This obviously doesn't work: it needs the proper setup! What is the correct way to implement this in functions.php?
Also: I'd like to know the strategy for figuring out how to implement functions like this in the future by myself? For example, I never know whether or not I'm supposed to use add_filter or do_action, and how I need to pass the parameters. I've yet to find a book or post out there that explains this very well, and can show me by example. Any good leads on this would be awesome too!
Start with the Wordpress codex. Visit the plugin API (which is really what you are doing) that explains Hooks, Actions and Filters. Then see the Action Reference which provides your list of hooks.
Here you will find the hook update_option_OPTIONNAME. Description from codex:
Runs after a WordPress option has been update by the update_option
function. Action function arguments: old option value, new option
value. You must add an action for the specific options that you want
to respond to, such as update_option_foo to respond when option "foo"
has been updated.
Adding code from asker's comment:
function inventory_linkurl_setting() {
update_option('image_default_link_type','none');
}
add_action('admin_init', 'inventory_linkurl_setting'); ?>

Wordpress wp_insert_post function

I am writing a php program to insert a wordpress post using wordpress functions. When I use the function wp_insert_post(), any <script> tags in my post content are removed. Is there is a way to override this in Wordpress?
The solution by pp19dd doesn't work anymore in newer versions of WordPress. The new way, as suggested by a WordPress developer (but not recommended, because of security issues):
filter had to be unset due to security problems with it. You could try doing kses_remove_filters() before inserting the post and kses_init_filters() after inserting the post if you are trying to avoid the kses filtering of the post fields. Just be wary since that defeats most of the security measures for inserting posts.
So:
kses_remove_filters();
wp_insert_post( $postdata );
kses_init_filters();
Source: https://wordpress.org/support/topic/bypass-sanitize_post-from-wp_insert_post

Resources