add third parameter to wordpress url - php

I have url http://domain.com/real-estate-news/phuket-luxury-property-in-high-demand-ms
Where "real-estate-news" is category and "phuket-luxury-property-in-high-demand-ms" is the post name .
when I print $wp_query->query_vars['name']; it gives the post slug and $wp_query->query_vars['category_name'] gives the category slug.
I want to generate a new url like http://domain.com/real-estate-news/phuket-luxury-property-in-high-demand-ms/xyz
and want to get xyz in query var like
echo $wp_query->query_vars['name']; // print "phuket-luxury-property-in-high-demand-ms"
echo $wp_query->query_vars['category_name']; // print "real-estate-news"
echo $wp_query->query_vars['section']; //print "xyz"
How to add section query var in wordpress please help me

Is this a Custom Post Type? If it is, you have more options than not. Within the register_post_type()'s array you can enter an argument for 'rewrite' to change the slug name and hack in specific rewrite rules for that particular CPT. I have put this project away because of its complexity and if you find an answer I'd love to hear it. Here is my notes on the matter.
register_post_type(array(
//options
'rewrite' => array(
'slug' => 'beach', //a slug used to identify the post type in URLs.
'with_front' => false,
'feed' => true,
'pages' => true
),
dreamdare.org
shibashake.com1
shibashake.com2
Beware of wp.tutsplus very often misinformation is offered there by authors themselves.
wp.tutsplus.com

The way I managed to do this is adding a rewrite rule.
This will look very similar to the one that custom post type creates but also receives a third parameter.
so in your case the rewrite rule would look like this
add_rewrite_rule(
// post-type | post-slug | section
'^real-state-news/([^/]+)(?:/([0-9]+))?/([^/]+)/?$',
// | Here the section is added
'index.php?post_type=real-state-news&name=$matches[1]&section=$matches[3]',
'top'
);
//You then need to add a tag to it
add_rewrite_tag('%section%','([^&]+)');
// At this point, you should be able to grab that param
get_query_var('section')

Related

How can I fire a custom function just once in Woocommerce

I need to add variations to my Woocommerce products programmatically and I borrowed the code from this answer thread:
Create programmatically a WooCommerce product variation with new attribute values
It works, but gives me two variations when I pass this data array:
$variation_data = array(
'attributes' => array(
'kidssize' => '2'
),
'sku' => '',
'regular_price' => '120',
'sale_price' => '',
'stock_qty' => '',
);
My guess is that funcntion fires two times.
Since i am a noob in php and backend in general all I know how to call a function is from some template file and visiting the page.
And to prevent other visitors from triggering it I use
if(isset($_GET['**parameter Only I know**']))
… wrap and call it going to the page with set parameter;
I understand that this is a really bad way for doing that, but how do I do it otherwise if I need to call function once and never use it again?
And is even firing twice a problem here or is it something wrong with my array?
EDIT:
Here's a detailed process of what i do:
i put the function from the link above in functions.php,
then put the call in footer.php of my theme with above mentioned wrap (the parameter is irrelevant - it could be anything, because it's just used as a trigger)
and go to the page with said parameter to trigger the call,
load the page only once and look for the result in admin panel.
And it has 2 variations always, even if i add more attributes to an array it will return 2 variatons of the last attributes array item;

How to get all posts except posts that belong to specific post type Wordpress?

For example, I register 3 post type (or more) in my theme by register_post_type() function:
Agenda
Download
Product
Now I want to get all posts that not belong to Product post type (all posts of Post, Page, Agenda, Download).
I don't want to use 'post_type' => array('post','page','agenda','download') because maybe I will register more post types in the future.
Isn't there no equivalent parameter to 'category__not_in' for post-types?
I'm not sure what are attempting is a great idea, but you could do something like this:
First, you get all the existing post_types:
$post_types = get_post_types(['public' => true]);
Then, you remove from that array the post types you don't want to query:
$excluded_posttypes = ['product'];
$post_types = array_diff($post_types, $excluded_posttypes);
And then you use that as a parameter for your WP_Query call:
'post_type' => $post_types,
Perhaps you'll need this: https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
There's an example for excluding categories, maybe this could work with post types.
In case it's not possible with the hook above, you could try the following before building your query:
Collect all the post types in your website: https://codex.wordpress.org/Function_Reference/get_post_types
Put them in an array so you'll be able to use that in your query for the 'post_type' option
Remove the post types that you want to exclude from the array
This way every newly registered post type will be included in the query. Take note that the default WP post type 'posts' will also be returned in the get_post_types function.

Wordpress: Change URL route of page

I'm new with Wordpress and I was asked to make some changes to a website. I need to change the following page's route (if possible):
/travels/poi
To:
/location/region/poi
Where travels is a listing of POIs (Points Of Interest). Is something like this possible?
EDIT: location/region/ is a page that lists my POIs
If travel is a custom post type, then you can define a custom permalink for it.
Looking for the code where you register the travel custom post type, something like this:
register_post_type( 'travels', $args );
Alter the rewrite argument:
$args = array(
// your ohter args
'rewrite' => array(
'slug' => 'location/region',
'with_front' => true,
'pages' => true,
'feeds' => true,
),
// some other args
);
After you made this changes, you have to flush the rewrite rules or the changes takes no effect. To do this, just go to Settings -> Permalinks in wp admin (you don't have to change anything).

Exclude a Wordpress category by name from a blog

I have a WP blog with a list of categories. I have specific template which is applied to a category called "News" which is all fine and working, but now I need to make sure that the posts in this category are not included in the main blog. I've tried a few bits and it hasn't worked out for me. Anyone any suggestions?
As I'm working off a dev/test/prod environment which were all set up by different people (... le sigh...) the same categories all have different id's so I was hoping to do it off the category name.
Cheers,
T
Thanks for that Giannis. I knew I could do it with the query_posts negative category type. Thanks for putting me on the right track though.
I have to work this off the category name, that's the only provision of this query. So to do this, I got the category id from the name:
$exclude = get_cat_ID('News');
$q = 'cat=-'.$exclude;
query_posts($q);
Put this at the top of the loop.php and solved my problem.
Thanks again everyone, love Stackoverflow!
Excluding a category from the loop is quite simple, you just need to pass the category ID as a parameter, for example to exclude categories with id 3 and 8:
<?php query_posts('cat=-3,-8'); ?>
In your case, it is not possible to exclude a category by its name (category_name parameter).
You can also try using a plugin if that's easier for the multiple environment situation:
http://wordpress.org/extend/plugins/simply-exclude/
I did it completely differenly, and using slugs only, as such:
$featured_args = array(
'post_type' => 'news', // I have a custom post type, 'news'
'tax_query' => array (
array(
'taxonomy' => 'subject', // I have a custom taxonomy, 'subject'
'field' => 'slug',
'terms' => array('politics', 'economy-and-business', 'disasters'),
'operator' => 'NOT IN' // This operator ensures that the values listed in 'term' are excluded
)
),
'post_status' => 'publish',
);
$featured_query = new WP_Query($featured_args);
Note that there exists an array entry, called tax_query, which also containts another array that containts a parameters called operator, and this operator has a value of NOT IN, which will exclude the values in the term field as opposed to including them.
IMPORTANT NOTE: Please be advised that if a post has two taxonomy terms, and one of those terms is in the list of terms to exclude, then this will not be part of the result.
For more details, read here: http://codex.wordpress.org/Class_Reference/WP_Query

Wordpress - use comment-system outside of pages and posts

so currently i'm using pods to create some individual pages for a log, filled with custom stuff.
now i want to use the comments-system for each of this pages e.g.:
mydomain.com/podpages/page1
mydomain.com/podpages/page2
mydomain.com/podpages/page3
this are not pages created with wordpress so simply adding <?php comments_template(); ?> is not working.
any ideas how to solve this problem?
thanks in advance
please leave a comment if something is unclear :)
When a comment is stored in the WordPress database, the ID of the post (or page) the comment relates to is also stored.
Trouble is, you're trying to save comments using WordPress, but for a page that it doesn't actually know about.
So, how about we create a WordPress page for each real page, but merely as a representation, so that your real pages and WordPress have a common ground for working with each other.
So, the plan here is to;
Load WordPress in the background on each of the 'real' pages.
See if a WordPress page representation already exists for the 'real' page
If it doesn't, create it, then and there
Trick WordPress into thinking we're actually viewing the representation
Carry on using all of WP's functions and 'template tags' as you would normally
This code should be somewhere at the beginning of the template file used to render your 'real' pages;
include ('../path/to/wp-load.php');
// remove query string from request
$request = preg_replace('#\?.*$#', '', $_SERVER['REQUEST_URI']);
// try and get the page name from the URI
preg_match('#podpages/([a-z0-9_-]+)#', $matches);
if ($matches && isset($matches[1])) {
$pagename = $matches[1];
// try and find the WP representation page
$query = new WP_Query(array('pagename' => $pagename));
if (!$query->have_posts()) {
// no WP page exists yet, so create one
$id = wp_insert_post(array(
'post_title' => $pagename,
'post_type' => 'page',
'post_status' => 'publish',
'post_name' => $pagename
));
if (!$id)
do_something(); // something went wrong
}
// this sets up the main WordPress query
// from now on, WordPress thinks you're viewing the representation page
}
UPDATE
I can't believe I was this stupid. Below should replace current code inside outer if;
// try and find the WP representation page - post_type IS required
$query = new WP_Query(array('name' => $pagename, 'post_type' => 'page'));
if (!$query->have_posts()) {
// no WP page exists yet, so create one
$id = wp_insert_post(array(
'post_title' => $pagename,
'post_type' => 'page',
'post_status' => 'publish',
'post_name' => $pagename,
'post_author' => 1, // failsafe
'post_content' => 'wp_insert_post needs content to complete'
));
}
// this sets up the main WordPress query
// from now on, WordPress thinks you're viewing the representation page
// post_type is a must!
wp(array('name' => $pagename, 'post_type' => 'page'));
// set up post
the_post();
P.S I think using the query_var name over pagename is better suited - it queries the slug, rather than the slug 'path'.
You'll also need to either place an input inside the form with name redirect_to and a value of the URL you'd like to redirect to, or, filter the redirect with a function hooked onto comment_post_redirect, returning the correct URL.
add
require('/path/to/wp-blog-header.php');
to include the wp files. this should give you all the functions/data you need.
Can you create pages in wordpress which display your log data? You might need a new template for this. WordPress will then have something to connect the comments to.
Do you need to use WordPress for this? If not, maybe something in this SO question helps: Unobtrusive, self-hosted comments function to put onto existing web pages
Just supply the wordpress-comment-part with a new ID - start with something your usual posts will never reach (100.000+ is your pages i.e.)
I don't know exactly if in wordpress it's a function (saveComment i.e.), but if it is so, just use it in your page with he custom ID.
You will nevertheless have to insert the Comments-form yourself.
And don't forget to modify the query that gets the news-entires that IDs over 100.000 are not entries.
Or you can write your own template that displays the standard-Worpress-stuff with IDs < 100.000, or else your pages.
Summed up, it should not be very difficult.
p.s.: If you just want to use the wordpress-login, then use any comment-system or make your own (it's an 1hour-thing) and authenticate / use the worpress-session.

Categories