On my blog page i have 2 default wordpress search forms, 1 in the header that should search for everything and 1 in the sidebar that should only search for posts.
i managed to change to query to suit my needs with pre_get_posts, but it changes it for both search forms
function exclude_pages($query) {
if ($query->is_home() && $query->is_main_query() && !is_admin() ) {
$query->set('post_type', 'post');
}
}
add_action('pre_get_posts', 'exclude_pages');
How can i specify that the function only changes the query for the search form in the sidebar?
Add a hidden field to one of your search forms then check if a value has been set for it in pre_get_posts.
Inside the sidebar form add:
<input type="hidden" name="posts-only" value="1" />
Then change this:
if ($query->is_home() && $query->is_main_query() && !is_admin() ) {
$query->set('post_type', 'post');
}
To:
// Check if posts only value has been set and equal to 1. Return if not.
if ( ! isset( $_GET['posts-only'] ) || 1 !== $_GET['posts-only'] ) {
return false;
}
if ( $query->is_home() && $query->is_main_query() && ! is_admin() ) {
$query->set( 'post_type', 'post') ;
}
Related
I know this should be obvious but my function is making all pages use index.php except the page using archive-project.php and the category pages for that post-type.
function change_project_loop( $query ) {
// Make sure this only fires when we want it too
if( !is_admin() || is_home() || is_page_template( 'archive-projects.php' ) && $query->is_main_query() && $query->is_tax('projectcat') ) {
// If so, modify the order variables
$query->set('meta_key', 'status' );
$meta_query[] = array(
array(
'key' => 'status',
'value' => 'assigned',
'compare' => 'NOT LIKE',
),
);
$query->set('meta_query',array( $meta_query ) );
}
}
add_action('pre_get_posts', 'change_project_loop', 9999);
I only want this to run on the home page, the page that uses archive-projects.php and the taxonomy pages for "projectcat".
You have incorrect if conditions.
if ( !is_admin() || is_home() || is_page_template( 'archive-projects.php' ) && $query->is_main_query() && $query->is_tax('projectcat') ) {
// ...
}
This means it is not admin page OR is home OR in template archive-projects.php
AND $query->is_main_query() AND $query->is_tax('projectcat') is true.
The !is_admin() is always be true if you use in theme/template for front pages even is_home() and is_page_template() is false but one of your condition is met.
And if all $query conditions are true then it means this if condition will always work on your front pages.
To fix this, remove !is_admin() because it is unnecessary, is_home() and is_page_template() will work for front pages already.
Also wrap your OR condition with bracket to group them.
if( (is_home() || is_page_template( 'archive-projects.php' )) && $query->is_main_query() && $query->is_tax('projectcat') ) {
// ...
}
Now, this means (it must be in home OR using specific template) - one of these conditions must be true.
AND all $query conditions must be true.
I'm trying to hide all pages except one specific from an user (ID=14). This is what I got so far with the help of this post »https://www.johnparris.com/how-to-hide-pages-in-the-wordpress-admin/«:
function jp_exclude_pages_from_user($query) {
if ( ! is_admin() )
return $query;
global $pagenow, $post_type;
if ( !$current_user->14 && $pagenow == 'edit.php' && $post_type == 'page' )
$query->query_vars['post__in'] = array( '10' ); // Enter your page IDs here
}
add_filter( 'parse_query', 'jp_exclude_pages_from_user' );
Result is that my site stops working.
There's an issue with your code. You are fetching user id outside the function. In that case the code should be as below:
function jp_exclude_pages_from_admin($query) {
if ( ! is_admin() )
return $query;
global $pagenow, $post_type;
$current_user_id = get_current_user_id();
if ( $current_user_id == 'youruserid' && $pagenow == 'edit.php' && $post_type == 'page' )
$query->query_vars['post__not_in'] = array( 'yourpageid' ); // Enter your page IDs here
}
add_filter( 'parse_query', 'jp_exclude_pages_from_admin' );
In the above section you forgot to put $current_user_id that's why it got suspended. Hopefully this code will work. Let me know if it works or not. I've tested it out & it works perfectly on my end
Why this code in functions.php has this strange side effect of switching the menu to the mobile version in not-home pages in wordpress?
function my_blog_category( $query ) {
if ( $query->is_home() && !is_front_page() || is_archive()) {
$query->set( 'cat', '6');
}
}
add_action( 'pre_get_posts', 'my_blog_category' );
This code should affect only posts in blog and archive page so why's that?
The reason it's not working is because you were asking:
If $query->is_home() && !is_front_page()
Or, is_archive()
Group your conditional expression using brackets.
function my_blog_category( $query ) {
if ( $query->is_home() && ( !is_front_page() || is_archive() ) ) {
$query->set( 'cat', '6');
}
}
add_action( 'pre_get_posts', 'my_blog_category' );
Now you're asking:
If $query->is_home()
And, if !is_front_page() or is_archive()
I have a search form containing select fields. The first two are populated with custom taxonomies and the third with the default wordpress categories. When using the first two only for a query it works fine. When I use the third( the categories), the search query just ignores the field and comes up with the same results. How can I fix this?
I've used these functions to make them work:
function ftiaxnospiti_filter_search($query) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( $query->is_search ) {
$query->set( 'post_type', array('post', 'seller') );
}
return $query;
};
add_action('pre_get_posts', 'ftiaxnospiti_filter_search');
function ftiaxnospiti_add_custom_types_to_tax( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
// Get all your post types
$post_types = array( 'post', 'seller' );
$query->set( 'post_type', $post_types );
}
return $query;
}
add_action( 'pre_get_posts', 'ftiaxnospiti_add_custom_types_to_tax' );
I want to add a new checkbox field inside Publish block in add/edit post page. Does anyone have idea how to do that ?
I have finally found the solution. I hope it will be of good use for somebody.
add_action( 'post_submitbox_misc_actions', 'publish_in_frontpage' );
function publish_in_frontpage($post)
{
$value = get_post_meta($post->ID, '_publish_in_frontpage', true);
echo '<div class="misc-pub-section misc-pub-section-last">
<span id="timestamp">'
. '<label><input type="checkbox"' . (!empty($value) ? ' checked="checked" ' : null) . 'value="1" name="publish_in_frontpage" /> Publish to frontpage</label>'
.'</span></div>';
}
function save_postdata($postid)
{
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
if ( !current_user_can( 'edit_page', $postid ) ) return false;
if(empty($postid) || $_POST['post_type'] != 'article' ) return false;
if($_POST['action'] == 'editpost'){
delete_post_meta($postid, 'publish_in_frontpage');
}
add_post_meta($postid, 'publish_in_frontpage', $_POST['publish_in_frontpage']);
}
rbncha's code didn't work out of the box and needed a lot of tweaking, the code below is what I came up with. I've added some comments which explains everything thoroughly.
The following code adds a checkbox in the publish block of posts (you can easily change the post type), and stores/retrieves the value in/from the database. With some minor tweaking you could easily add a text field or anything you like.
It should be noted that you have to change my_ to a unique key for your theme or plugin!
add_action( 'post_submitbox_misc_actions', 'my_featured_post_field' );
function my_featured_post_field()
{
global $post;
/* check if this is a post, if not then we won't add the custom field */
/* change this post type to any type you want to add the custom field to */
if (get_post_type($post) != 'post') return false;
/* get the value corrent value of the custom field */
$value = get_post_meta($post->ID, 'my_featured_post_field', true);
?>
<div class="misc-pub-section">
<?php //if there is a value (1), check the checkbox ?>
<label><input type="checkbox"<?php echo (!empty($value) ? ' checked="checked"' : null) ?> value="1" name="my_featured_post_field" /> Featured on frontpage</label>
</div>
<?php
}
add_action( 'save_post', 'my_save_postdata');
function my_save_postdata($postid)
{
/* check if this is an autosave */
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
/* check if the user can edit this page */
if ( !current_user_can( 'edit_page', $postid ) ) return false;
/* check if there's a post id and check if this is a post */
/* make sure this is the same post type as above */
if(empty($postid) || $_POST['post_type'] != 'post' ) return false;
/* if you are going to use text fields, then you should change the part below */
/* use add_post_meta, update_post_meta and delete_post_meta, to control the stored value */
/* check if the custom field is submitted (checkboxes that aren't marked, aren't submitted) */
if(isset($_POST['my_featured_post_field'])){
/* store the value in the database */
add_post_meta($postid, 'my_featured_post_field', 1, true );
}
else{
/* not marked? delete the value in the database */
delete_post_meta($postid, 'my_featured_post_field');
}
}
If you want to read more about custom fields see here: http://codex.wordpress.org/Custom_Fields
Well!, I could not find a solution to add a field in Publish Block. For the temporary solution, I have added new block by simply adding simple codes like below.
add_action( 'admin_init', 'category_metabox');
//add new publish to frontpage box
add_meta_box(
'publish_in_frontpage',
'Publish in Frontpage',
'publish_in_frontpage_callback',
'article',
'side',
'high'
);
function publish_in_frontpage_callback($post)
{
$value = get_post_meta($post->ID, '_publish_in_frontpage', true);
echo '<label><input type="checkbox"' . (!empty($value) ? ' checked="checked" ' : null) . 'value="1" name="publish_in_frontpage" /> Publish to frontpage</label>';
}
add_action( 'save_post', 'save_postdata');
function save_postdata($postid)
{
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
if ( !current_user_can( 'edit_page', $postid ) ) return false;
if(empty($postid) || $_POST['post_type'] != 'article' ) return false;
if($_POST['action'] == 'editpost'){
delete_post_meta($postid, 'publish_in_frontpage');
}
add_post_meta($postid, 'publish_in_frontpage', $_POST['publish_in_frontpage']);
}
Use the Advanced Custom Fields plugin for wordpress.