I would like to apply my own logic in WordPress that'll determine if the current page is the front page or not and then I want is_front_page() to return that result. Is there anyway to hook into that function or use the 'pre_get_posts' action to change the necessary values so that is_front_page() is affected? is_front_page() needs to use my custom logic because I'm using other 3rd party plugins that uses that function and I don't want to modify those plugins so that when I update them in the future, my site won't break. Has someone needed to do this before and/or could show me what variables of the WP_Query object I need to change? Thanks!
There is no way to "hook" into is_front_page(), you're going to have to create your own function and use that instead.
The function itself is very simple, I've included it for you below - use this as a starting point for something new and put it in your functions.php file.
public function is_front_page() {
// most likely case
if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
return true;
elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
return true;
else
return false;
}
Related
I'm using an event management plugin, and I want to display some specific event details via shortcodes.
I've found this very useful guide regarding this plugin's shortcodes:
https://urjtechhelp.zendesk.com/hc/en-us/articles/115002801594-Embedding-Single-Events-with-the-tribe-event-inline-Shortcode
But I have one problem. As stated in the guide you must always supply an event’s ID via the id shortcode attribute, like this:
[tribe_event_inline id="167"]
What I'm trying to achieve is for the shortcode to always use the ID of the post it's placed in.
I've tried adding additional shortcode
add_shortcode( 'return_post_id', 'the_dramatist_return_post_id' );
function the_dramatist_return_post_id() {
return get_the_ID();
}
And then nest the shortcode in the original one, but apparently it doesn't work that way.
Any idea on how to achieve this?
Thank you in advance for any ideas.
Maybe it's help:
add_shortcode( 'return_post_id', 'the_dramatist_return_post_id' );
function the_dramatist_return_post_id() {
global $post;
return $post->ID ?? '';
}
This would not work using the WP text editor. It would also not be a clean implementation.
I would recommend going straight to the problem and edit the function responsible for the "tribe_event_inline" shortcode.
Try searching for add_shortcode('tribe_event_inline' in your source code files.
Here is one my function what I use time to time. I simplify it for you but with small work can be expanded to more levels.
function current_ID($post_id=NULL){
global $product, $page;
if(is_numeric($post_id) && $post_id == intval($post_id)) {} else {
// Get ID from the current object
if(!is_object($post_id)) {} else if(property_exists($post_id, 'ID')) {
$post_id = $post_id->ID;
}
// Woocommerce product page
if(empty($post_id) && property_exists($product, 'ID')) $post_id = $product->ID;
// Standard page/post ID
if(empty($post_id)) $post_id = get_the_ID();
// Get ID from the $page global
if(empty($post_id) && property_exists($page, 'ID')) $post_id = $page->ID;
// GET request
if(isset($_GET['post']) && is_numeric($_GET['post']) && $_GET['post'] > 0 && intval($_GET['post']) == $_GET['post']) $post_id = intval($_GET['post']);
if(isset($_GET['p']) && is_numeric($_GET['p']) && $_GET['p'] > 0 && intval($_GET['p']) == $_GET['p']) $post_id = intval($_GET['p']);
// Get ID from the get_queried_object_id() function
if(empty($post_id) && function_exists('get_queried_object_id')) $post_id = get_queried_object_id(); // Need tests
}
// The end
return $post_id;
}
It support also Woocommerce.
Now you need to include it into your project and add it to shortcode or combine with some other function to accept it.
Idea:
Use a template for your shortcode like [tribe_event_inline id="ZXZX"]
Add filter for the_content.
Use your filter to replace ZXZX with post ID.
I would like:
http://www.gadgetgogo.co.uk/?s=ipod
to return as:
http://www.gadgetgogo.co.uk/?s=ipod&post_type=product
So when using searches (slider banner and default WordPress search) it produces the second URL.
I was looking for similar solution as you but couldn't find any and at last I combined last few answers that I read here and got this working fine as I wanted.
Add below code in your theme's functions.php file
function wpb_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) && ($_GET['post_type'] != 'product') ) {
wp_redirect( home_url( "/?s=" ) . urlencode( get_query_var( 's' ) ) . "&post_type=product" );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
hope it will help you and others.
Apparently current WooCommerce versions only consider a search query a product search (and render using the appropriate template), if $query->is_post_type_archive( 'product' ) is true, so the key is to set not only the post_type, but the is_post_type_archive property as well, and to do it before WooCommerce loads its filter (default priority of 10), so with a priority of 9 or smaller.
Example to add into funtions.php:
function my_search_filter($query) {
if ( $query->is_search && ! is_admin() ) {
$query->set( 'post_type', 'product' );
$query->is_post_type_archive = true;
}
}
add_filter('pre_get_posts','my_search_filter', 9);
Please note, that this code will override all searches as product serach, so if you have other searches as well, implement appropriate checks at the begining of my_search_filter.
Just add this line to top of search.php
$_GET['post_type'] = 'product'
This can be done using pre_get_posts filter. Add below code in your theme's functions.php file
add_filter( 'pre_get_posts', 'search_by_product_only' );
function search_by_product_only( $query ) {
// check if search query only
if ( $query->is_search ) {
$query->set( 'post_type', array( 'product') ); // here you can add multiple post types in whcih you want to search
}
return $query;
}
I currently have this php snippet in a WordPress-based website:
if ( ! is_singular( 'page' ) || ! current_theme_supports( 'genesis-after-entry-widget-area' ) ) {
return;
}
I would like to have some type of "or" clause, where I can tell the server, "if this item is a page or a portfolio type, then return."
Basically, would like to include the types: "page" and "portfolio" in this snippet.
But I am unsure how to do this without redeclaring the function all over again.
Any guidance would be greatly appreciated!
I think what are you looking for is WordPress get_post_type function:
if (get_post_type() == 'page' || get_post_type() == 'portfolio') {
return;
}
I'm not a wordpress developer, but based on their is_singular documentation, it seems that the function accepts an array of custom post type values. So you should be able to do something like this:
if (is_singular(array('page', 'portfolio'))) {
return;
}
when i add this code to my functions.php
function meta_filter_posts( $query )
{
if(is_tag() && is_main_query())
{
$currentTagId = get_queried_object()->term_id;
$query->set('orderby','meta_value_num');
$query->set('meta_key', 'rank_tag_'.$currentTagId.'');
$query->set('order', 'ASC');
}
}
add_filter( 'pre_get_posts', 'meta_filter_posts' );
my sidebar custom menu-widgets doesnt work anymore. The widgets show only the widget-title but not the widget-content.
The rest, e.g. the text-widgets are working normal.
But why? What is wrong with my code?
You have two problems here:
When using pre_get_posts, you should always make sure that you target the front end only. pre_get_posts alters all type of queries front end and backend
is_tag() and is_main_query() should be member variables of $query
You can do something like this
if(!is_admin() && $query->is_tag() && $query->is_main_query())
How can I hide posts from some category with some ID from main page of my site? I need solution like filter:
function exclude_post($query) {
if ($query->is_home) {
...
}
return $query;
}
add_filter('pre_get_posts', 'exclude_post');
Can somebody provide an example?
Use $query->set( $query_var, $value ); where $query_var is the variable you want to add/update in query. So put this inside your condition:
// 1st parameter is the query variable the 2nd is its value, in this case an array of category IDs
$query->set( 'category__not_in', array( 2, 6 ) );
Remember is good practice put in condition a check to $query->is_main_query(). pre_get_posts is an action hook so you have to change add_filter to add_action.
An action hook doesn't return a value, a filter does.
Example
function exclude_post( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'category__not_in', array( 2, 6 ) );
}
}
add_action('pre_get_posts', 'exclude_post');
UPDATE
According to the new details emerging by the question,in order to exclude some posts in feeds stream, but not in category archive, the conditional check might look like:
if( $query->is_feed() && !$query->is_archive() )
OR
if( $query->is_feed() && !$query->is_category() )
Hope it helps!
you can also use the following way to exclude the a category from post query
<?php
if ( is_home() )
{
query_posts($query_string . '&cat=-3');
}
?>
You can use simple exclude in the query parameters :-
<?php wp_list_categories('orderby=name&show_count=1&exclude=10'); ?>
Its only sample , how to you exclude.
Hop it work..